Octocontrabass wrote: ↑Thu Jul 03, 2025 7:48 pm
I'm especially interested in the parameters being passed to utos().
My implementation of utos() takes the value, number base and output buffer, and the code itself is as follows:
Code: Select all
void utos(uint32_t val, uint8_t base, char *buf) {
uint32_t n = 0;
uint32_t temp = val;
if(val == 0) {
return "0";
}
else {
while(temp > 0) {
n++;
temp /= base;
}
}
buf[n] = '\0';
char charmap[32] = "0123456789abcdefghijklmnopqrstu";
unsigned int strptr = n-1;
while(val > 0) {
char digit = charmap[val % base];
val /= base;
buf[strptr] = digit;
strptr--;
}
}
I do not believe the behaviour of this function is relevant to the issue, as the issue still occurs in other scenarios, such as dynamically allocated strings.
Interestingly, moving the testing code to before reading the multiboot information structure makes the numbers appear properly in the serial terminal, however it causes a page fault.
Is it possible that my page allocation code may be faulty? I have the source for that below:
Code: Select all
uint8_t get_frame_status(int frameid) {
uint32_t index = frameid / 8;
uint32_t shift = frameid % 8;
uint8_t byte = framemap[index];
return (byte & (1 << shift)) >> shift;
}
void set_frame_status(int frameid, uint8_t s) {
uint32_t index = frameid / 8;
uint32_t shift = frameid % 8;
framemap[index] |= (s & 1) << shift;
}
void* kalloc_alloc(int n) {
uint32_t first_frame = (end_kernel) / 4096 + 1;
uint32_t index = 0;
while(index < NPAGES) {
uint8_t success = 1;
for(int i = 0; i < n; ++i) {
if(get_frame_status(index + i)) {
success = 0;
break;
}
}
if(success) {
for(int i = 0; i < n; ++i) {
set_frame_status(index + i, 1);
}
return (void*)(first_frame + (index * 4096));
}
index++;
}
return (void*)0;
}
void kalloc_free(void* ptr, int n) {
uint32_t first_frame = (end_kernel + 4096) / 4096;
uint32_t index = ((uint32_t)ptr - first_frame) / 4096;
for(int i = 0; i < n/8; ++i) {
for(int j = 0; j < 8; ++j) {
set_frame_status(index+8*i+j, 0);
}
}
}