Add 64bit printf

This commit is contained in:
Mathieu Maret 2021-01-23 21:21:13 +01:00
parent 774a20b8c8
commit c3d345bb87
5 changed files with 68 additions and 19 deletions

View File

@ -20,7 +20,7 @@ cobj=$(csrc:%.c=%.o) arch/$(ARCH)/cpu_context_switch.o arch/$(ARCH)/irq_pit.o
deps = $(csrc:%.c=%.d)
kernel:$(asmobj) $(cobj) linker.ld
$(CC) -m32 -ffreestanding -nostdlib $(cobj) $(asmobj) -o $@ -T linker.ld
$(CC) -m32 -ffreestanding -nostdlib $(cobj) $(asmobj) -o $@ -T linker.ld -lgcc
objcopy --only-keep-debug $@ $@.sym
objcopy --strip-debug $@

View File

@ -33,7 +33,7 @@ void *memset(void *src, int c, size_t n)
return src;
}
char *itoa(int value, char *str, int base)
char *itoa(long long int value, char *str, int base)
{
char *rc;
char *ptr;
@ -148,24 +148,47 @@ void putc(const char str)
serialPutc(str);
}
void printInt(int integer)
void printInt64(long long int integer)
{
char num[sizeof(int) * 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
char num[sizeof(integer) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0, k = 0;
int i = 0;
if (integer < 0) {
putc('-');
}
if (integer == 0) {
num[i++] = 0;
}
while (integer != 0) {
do {
int digit = integer % 10;
num[i++] = (digit > 0) ? digit : -digit;
integer = integer / 10;
} while (integer != 0);
for (i = i - 1; i >= 0; i--) {
putc(num[i] + '0');
}
for (k = i - 1; k >= 0; k--) {
putc(num[k] + '0');
}
void printInt(int integer)
{
char num[sizeof(integer) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0;
if (integer < 0) {
putc('-');
}
do {
int digit = integer % 10;
num[i++] = (digit > 0) ? digit : -digit;
integer = integer / 10;
} while (integer != 0);
for (i = i - 1; i >= 0; i--) {
putc(num[i] + '0');
}
}
@ -205,6 +228,35 @@ void vprintf(const char *format, va_list ap)
case '%':
putc('%');
break;
case 'l':
switch (format[i + 2]) {
case 'l':
switch (format[i + 3]) {
case 'd': {
long long int d = va_arg(ap, long long int);
printInt64(d);
break;
}
case 'p':
case 'x': {
char val[sizeof(long long int) * 2];
long long int d = va_arg(ap, long long int);
itoa(d, val, 16);
puts(val);
break;
}
}
i++;
break;
case 'i':
case 'd': {
long int d = va_arg(ap, long int);
printInt64(d);
break;
}
}
i++;
break;
}
i++;
break;

View File

@ -13,7 +13,7 @@
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
char *itoa(int value, char *str, int base);
char *itoa(long long int value, char *str, int base);
void reverse(char s[]);
int strlen(const char s[]);
unsigned int strnlen(const char *s, size_t count);
@ -21,7 +21,6 @@ int strcmp(const char s1[], const char s2[]);
char *strzcpy(char *dst, const char *src, int len);
void puts(const char *str);
void putc(const char str);
void printInt(int integer);
void vprintf(const char *format, va_list ap);
void printf(const char *format, ...);

View File

@ -99,10 +99,8 @@ void kmain(unsigned long magic, unsigned long addr)
uint size = mbi->mmap_length / sizeof(struct multiboot_mmap_entry);
pr_devel("mmap buffer at 0x%x with %d entries\n", mbi->mmap_addr, size);
for (uint i = 0; i < size; i++) {
printf(" base_addr 0x%x 0x%x, length = 0x%x 0x%x, type = 0x%x\n",
(unsigned)(mmap[i].addr >> 32), (unsigned)(mmap[i].addr & 0xffffffff),
(unsigned)(mmap[i].len >> 32), (unsigned)(mmap[i].len & 0xffffffff),
(uint32_t)mmap[i].type);
printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr,
mmap[i].len, (uint32_t)mmap[i].type);
memAddBank(
max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem),
min((multiboot_uint64_t)(upperMemKB * 1024), mmap[i].addr + mmap[i].len),

View File

@ -19,7 +19,7 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024);
unsigned long nbPage = ((upperMemKB) / (PAGE_SIZE / 1024));
printf("Available Mem from 0x%x to 0x%x: %dMB and %dPages(%d)\n", &__ld_kernel_end,
printf("Available Mem from 0x%x to 0x%x: %dMB in %d Pages of %dB\n", &__ld_kernel_end,
upperMemKB * 1024, (upperMemKB * 1024 - (uint32_t)&__ld_kernel_end) / (1024 * 1024),
nbPage, PAGE_SIZE);