Skip to content

Commit

Permalink
Fix #48 and upgrade info buddy
Browse files Browse the repository at this point in the history
In bitmap_ffu, use a bitmap_t(uint8_t) variable to get the inverted byte
and call __builtin_ffs.
The old way used a pointer and called `__builtin_ffs(~*cur)` that
casted the value in 32bit so the __builtin_ffs returned a index bigger
than the number of blocks.

Add to info buddy a optional arg to print the order.

Reset the color in alloc when allocation failed.

Check on size of alloc and free argument to return an error if negative.
Also check the address argument of free builtin to return an error if
not well formatted.

Signed-off-by: xlmod <[email protected]>
Co-authored-by: mrxx0 <[email protected]>
  • Loading branch information
xlmod and mrxx0 committed Dec 19, 2022
1 parent 69370be commit bed61f7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
16 changes: 8 additions & 8 deletions kernel/memory/kpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Kernel Physical Memory management
*
* created: 2022/11/23 - lfalkau <[email protected]>
* updated: 2022/12/15 - mrxx0 <chcoutur@student.42.fr>
* updated: 2022/12/17 - xlmod <glafond-@student.42.fr>
*/

#include <kernel/kpm.h>
Expand Down Expand Up @@ -58,8 +58,8 @@ void kpm_init(struct multiboot_mmap_entry *entries, size_t count, size_t memkb)
kpm_enable((void *)(uintptr_t)entry->addr, (uint32_t)(entry->len));
}
kpm_disable((void *)0, PAGE_SIZE); // Also disables IDT + GDT by design
kpm_disable(&sk, (void *)(&ek - KERNEL_VIRT_OFFSET) - (void *)&sk);
kpm_disable(buddy - KERNEL_VIRT_OFFSET, buddy->size);
kpm_disable(&sk, ((uintptr_t)&ek - KERNEL_VIRT_OFFSET) - (uintptr_t)&sk);
kpm_disable((void *)((uintptr_t)buddy - KERNEL_VIRT_OFFSET), buddy->size);
}

/*
Expand Down Expand Up @@ -186,8 +186,8 @@ static int bitmap_ffu(bitmap_t *bitmap, size_t size) {
int ffu;

for (size_t i = 0; i < size; i++) {
bitmap_t *cur = bitmap + i;
ffu = __builtin_ffs(~*cur);
bitmap_t cur = ~bitmap[i];
ffu = __builtin_ffs(cur);
if (ffu)
return ffu + (i * 8) - 1;
}
Expand All @@ -210,10 +210,10 @@ int kpm_alloc(kpm_chunk_t *chunk, size_t size) {
size_t frames_per_block;

best_fit_order = find_best_fit_order(size);
for (int n = best_fit_order; n >= 0; n--) {
int i = bitmap_ffu(buddy->orders[n].bitmap, buddy->orders[n].size);
for (int o = best_fit_order; o >= 0; o--) {
int i = bitmap_ffu(buddy->orders[o].bitmap, buddy->orders[o].size);
if (i >= 0) {
frames_per_block = 1 << n;
frames_per_block = 1 << o;
base_index = i * frames_per_block;
for (size_t j = 0; j < frames_per_block; j++)
KPM_ALLOC(0, base_index + j);
Expand Down
12 changes: 7 additions & 5 deletions kernel/nsh/builtins/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Allocate builtin file
*
* created: 2022/12/13 - glafond- <[email protected]>
* updated: 2022/12/16 - lfalkau <lfalkau@student.42.fr>
* updated: 2022/12/19 - mrxx0 <chcoutur@student.42.fr>
*/

#include <kernel/kpm.h>
Expand All @@ -32,14 +32,16 @@ int alloc_loop(kpm_chunk_t *chunk, size_t size) {
uint8_t oldcolor = sb_get_color(sb_current);
sb_set_fg(sb_current, SB_COLOR_GREEN);
while (size > 0) {
if (kpm_alloc(chunk, size) < 0)
if (kpm_alloc(chunk, size) < 0) {
sb_set_color(sb_current, oldcolor);
return -1;
}
kprintf("chunk: {\n addr = %p\n size = %u\n}\n", chunk->addr, chunk->size);
if (chunk->size >= size)
break ;
size -= chunk->size;
}
sb_set_fg(sb_current, oldcolor);
sb_set_color(sb_current, oldcolor);
return 0;
}

Expand All @@ -58,8 +60,8 @@ int alloc(int argc, char **argv) {
return -1;
}
char *ptr;
int size = strtol(argv[1], &ptr, 0);
if (*ptr != 0) {
int32_t size = strtol(argv[1], &ptr, 0);
if (*ptr != 0 || size < 0) {
kprintf(BLTNAME ": Size not well formatted.\n");
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions kernel/nsh/builtins/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Free builtin file
*
* created: 2022/12/15 - mrxx0 <[email protected]>
* updated: 2022/12/15 - mrxx0 <[email protected]>
* updated: 2022/12/19 - mrxx0 <[email protected]>
*/

#include <kernel/kpm.h>
Expand All @@ -33,12 +33,12 @@ int free(int argc, char **argv) {
}
char *ptr;
uint32_t addr = strtol(argv[1], &ptr, 0);
if (*ptr != 0) {
if (*ptr != 0 || argv[1][0] == '-') {
kprintf(BLTNAME ": Address not well formatted.\n");
return -1;
}
size_t size = strtol(argv[2], &ptr, 0);
if (*ptr != 0) {
int32_t size = strtol(argv[2], &ptr, 0);
if (*ptr != 0 || size < 0) {
kprintf(BLTNAME ": Size not well formatted.\n");
return -1;
}
Expand Down
48 changes: 44 additions & 4 deletions kernel/nsh/builtins/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Info builtin file
*
* created: 2022/12/09 - mrxx0 <[email protected]>
* updated: 2022/12/15 - glafond- <[email protected]>
* updated: 2022/12/17 - xlmod <[email protected]>
*/

#include <stdint.h>
#include <kernel/print.h>
#include <kernel/string.h>
#include <kernel/kpm.h>
#include <kernel/screenbuf.h>
#include <kernel/stdlib.h>

#include "../../gdt_internal.h"
#include "../../idt_internal.h"
Expand All @@ -26,6 +28,8 @@ extern buddy_t *buddy;
extern t_idt_entry idt[256];
extern t_idt_ptr idtp;

extern struct screenbuf *sb_current;

#define PRINT(reg, var, format) \
reg(var); kprintf(format, var)

Expand Down Expand Up @@ -62,13 +66,39 @@ static void info_idt() {
kprintf("Size = %u bytes\n", sizeof(idt));
}

static void info_buddy() {
static void info_buddy_print_order(int order) {
uint8_t oldcolor = sb_get_color(sb_current);
kprintf("order %u(size of %u)\n", order, buddy->orders[order].size);
for (size_t i = 0; i < buddy->orders[order].size; i++) {
if (i % 32 == 0) {
kprintf("\n%4x ", i);
}
bitmap_t val = buddy->orders[order].bitmap[i];
if (val == 0) {
sb_set_bg(sb_current, SB_COLOR_WHITE);
kprintf(" ");
} else if (val == 0xff) {
sb_set_bg(sb_current, SB_COLOR_RED);
kprintf(" ");
} else {
sb_set_bg(sb_current, SB_COLOR_BROWN);
kprintf("%2x", val);
}
sb_set_color(sb_current, oldcolor);
}
sb_set_color(sb_current, oldcolor);
kprintf("\n");
}

static void info_buddy(int order) {
kprintf("INFO BUDDY\n");
kprintf("buddy address: %p\n", buddy);
kprintf("buddy size: %u KB\n", buddy->size / 1024);
kprintf("orders address: %p\n", buddy->orders[0].bitmap);
kprintf("frame number: %x\n", buddy->nframes);
kprintf("memory size: %u KB\n\n", buddy->nframes << 2);
kprintf("memory size: %u KB\n", buddy->nframes << 2);
if (order >= 0)
info_buddy_print_order(order);
}

static void info_stack() {
Expand Down Expand Up @@ -103,7 +133,17 @@ int info(int argc, char **argv) {
} else if (!strcmp(argv[1], "stack")) {
info_stack();
} else if (!strcmp(argv[1], "buddy")) {
info_buddy();
if (argc > 2) {
char *ptr;
int order = strtol(argv[2], &ptr, 0);
if (*ptr || order < 0 || order >= KPM_NORDERS) {
kprintf(BLTNAME ": order number wrong.\n");
return -1;
}
info_buddy(order);
} else {
info_buddy(-1);
}
} else if (!strcmp(argv[1], "idt")) {
info_idt();
} else if (!strcmp(argv[1], "registers")) {
Expand Down

0 comments on commit bed61f7

Please sign in to comment.