From f94d2f20dbbeee9dc8329bf72000c9fa1921ea2e Mon Sep 17 00:00:00 2001 From: uosfz Date: Thu, 27 Feb 2025 14:04:19 +0100 Subject: [PATCH 1/3] non-standard format specifier X8-X64 --- src/kernel.c | 2 ++ src/std.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/kernel.c b/src/kernel.c index 2a00e58..f7cf3e9 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -130,6 +130,8 @@ void _start() { __asm__("int $0x80" :: ); printf("Test after interrupt\n"); + printf("%X8 %X16 %X32 %X64\n", -1, 2, 3, 1ull<<33); + // hang for now PANIC("end of kernel"); } diff --git a/src/std.c b/src/std.c index 9df9dfa..94a6400 100644 --- a/src/std.c +++ b/src/std.c @@ -304,6 +304,32 @@ void printf(const char *format, ...) { putu64x((uint64_t)(intptr_t)ptr); break; } + case 'X': { + char size1 = *(++format); + ASSERT(size1 != '\0'); + if (size1 == '8') { + uint8_t val = (uint8_t)va_arg(args, int); + putu8x(val); + break; + } + char size2 = *(++format); + ASSERT(size2 != '\0'); + if (size1 == '1' && size2 == '6') { + uint16_t val = (uint16_t)va_arg(args, int); + putu16x(val); + break; + } else if (size1 == '3' && size2 == '2') { + uint32_t val = (uint32_t)va_arg(args, int); + putu32x(val); + break; + } else if (size1 == '6' && size2 == '4') { + uint64_t val = (uint64_t)va_arg(args, long long); + putu64x(val); + break; + } else { + PANIC("printf: invalid X size"); + } + } case '%': { putc('%'); break; From c8b445c59147db4d797c2328129e4036389a4b47 Mon Sep 17 00:00:00 2001 From: uosfz Date: Thu, 27 Feb 2025 14:10:18 +0100 Subject: [PATCH 2/3] replaced putu* with printf call --- src/kernel.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/kernel.c b/src/kernel.c index f7cf3e9..5814829 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -46,9 +46,7 @@ void print_virtio_blk_bars() { if (pci_search(0x1AF4, 0x1001, &bdf) && pci_search(0x1AF4, 0x1042, &bdf)) { PANIC("couldn't find virtio_blk device!"); } - puts("found virtio_blk device at bdf "); - putu16x(bdf); - printf(".\n"); + printf("found virtio_blk device at bdf %X16.\n", bdf); // read BARs for (int i = 0; i < 6; i++) { struct pci_bar_desc desc = pci_bar_desc_read(bdf, i); @@ -62,8 +60,7 @@ void check_initrd() { for (uint64_t off = 0; off < bootboot.initrd_size; off++) { uint8_t byte = ((uint8_t*)bootboot.initrd_ptr)[off]; - putu8x(byte); - putc(' '); + printf("%X8 ", byte); if (++printed_in_line % 32 == 0) { printed_in_line = 0; @@ -118,9 +115,7 @@ void _start() { struct tar_header hd; int res = tar_get_file("hello.txt", &hd); ASSERT(res == 1); - puts(hd.name); - putu32x(hd.size); - putln(); + printf("%s %X32\n", hd.name, hd.size); for (uint64_t i = 0; i < hd.size; i++) { putc(((char *)hd.data)[i]); } From 15ab75ffe40bd6bdd7f5605600f7b182cfb0c811 Mon Sep 17 00:00:00 2001 From: uosfz Date: Thu, 27 Feb 2025 18:42:50 +0100 Subject: [PATCH 3/3] fix struct packing, weird formatting --- src/pci.c | 8 ++++---- src/x86_64/mem.c | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/pci.c b/src/pci.c index 8eae46d..938bdb8 100644 --- a/src/pci.c +++ b/src/pci.c @@ -33,8 +33,8 @@ void pci_config_write_u32(uint16_t bdf, uint8_t offset, uint32_t value) { struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) { ASSERT(barnum < 6); - // get basic information - // https://en.wikipedia.org/wiki/PCI_configuration_space + // get basic information + // https://en.wikipedia.org/wiki/PCI_configuration_space uint8_t offset = 0x10 + (barnum << 2); uint32_t bar = pci_config_read_u32(bdf, offset); struct pci_bar_desc desc; @@ -48,8 +48,8 @@ struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) { desc.address = bar & 0xfffffffc; } - // get length - // https://stackoverflow.com/questions/19006632/how-is-a-pci-pcie-bar-size-determined + // get length + // https://stackoverflow.com/questions/19006632/how-is-a-pci-pcie-bar-size-determined pci_config_write_u32(bdf, offset, 0xffffffff); bar = pci_config_read_u32(bdf, offset); desc.length = (~(bar & 0xfffffff0)) + 1; diff --git a/src/x86_64/mem.c b/src/x86_64/mem.c index 1e9f4f8..369d21e 100644 --- a/src/x86_64/mem.c +++ b/src/x86_64/mem.c @@ -8,14 +8,12 @@ void write_segment_descriptor(uint64_t *entry, uint8_t dpl, uint8_t executable) | (dpl << 5) | (1 << 4) // S | (executable << 3) | (0 << 2) // DC | (1 << 1) // RW - | (0 << 0) // A - ; + | (0 << 0); // A uint8_t flags = (1 << 3) // G | (0 << 2) // DB | (1 << 1) // L - | (0 << 0) // reserved - ; + | (0 << 0); // reserved *entry = 0xffff // limit | ((uint64_t)0x0000 << 16) // base @@ -23,8 +21,7 @@ void write_segment_descriptor(uint64_t *entry, uint8_t dpl, uint8_t executable) | ((uint64_t)access_byte << 40) // access byte | ((uint64_t)0xf << 48) // limit | ((uint64_t)flags << 52) // flags - | ((uint64_t)0x00 << 56) // base - ; + | ((uint64_t)0x00 << 56); // base } #define CODE_SEGMENT 1 @@ -43,11 +40,12 @@ void init_gdt() { __asm__("lgdt (%0)" ::"r"(gdtr)); __asm__("mov %0, %%ds" ::"r"(DATA_SEGMENT << 3)); - // TODO set code segment -> iret? + + // TODO set code segment -> iret? } -__attribute__ ((packed)) -struct int_desc_entry { +struct __attribute__((packed)) +int_desc_entry { uint16_t offset1; uint16_t selector; uint8_t ist; @@ -63,8 +61,7 @@ struct int_desc_entry { void write_segment_selector(uint16_t *ss, uint16_t index) { *ss = (uint16_t)PRIV_KERNEL | 0 // use GDT - | (index << 3) - ; + | (index << 3); } #define GATE_TYPE_INTERRUPT 0xe @@ -74,9 +71,8 @@ void write_int_desc_entry(struct int_desc_entry *e, uint64_t offset) { write_segment_selector(&e->selector, CODE_SEGMENT); e->ist = 0; e->attr = (uint8_t)GATE_TYPE_INTERRUPT - | (uint8_t)(PRIV_KERNEL << 5) - | (uint8_t)(1 << 7) // present - ; + | (uint8_t)(PRIV_KERNEL << 5) + | (uint8_t)(1 << 7); // present e->offset2 = (uint16_t) ((offset >> 16) & 0xffff); e->offset3 = (uint32_t) ((offset >> 32) & 0xffffffff); e->pad = 0;