fix struct packing, weird formatting

This commit is contained in:
uosfz 2025-02-27 18:42:50 +01:00
parent c8b445c591
commit 15ab75ffe4
Signed by: uosfz
SSH key fingerprint: SHA256:FlktuluyhTQg3jHZNLKwxOOC5hbfrUXM0tz3IA3lGJo
2 changed files with 14 additions and 18 deletions

View file

@ -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) { struct pci_bar_desc pci_bar_desc_read(uint16_t bdf, uint8_t barnum) {
ASSERT(barnum < 6); ASSERT(barnum < 6);
// get basic information // get basic information
// https://en.wikipedia.org/wiki/PCI_configuration_space // https://en.wikipedia.org/wiki/PCI_configuration_space
uint8_t offset = 0x10 + (barnum << 2); uint8_t offset = 0x10 + (barnum << 2);
uint32_t bar = pci_config_read_u32(bdf, offset); uint32_t bar = pci_config_read_u32(bdf, offset);
struct pci_bar_desc desc; 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; desc.address = bar & 0xfffffffc;
} }
// get length // get length
// https://stackoverflow.com/questions/19006632/how-is-a-pci-pcie-bar-size-determined // https://stackoverflow.com/questions/19006632/how-is-a-pci-pcie-bar-size-determined
pci_config_write_u32(bdf, offset, 0xffffffff); pci_config_write_u32(bdf, offset, 0xffffffff);
bar = pci_config_read_u32(bdf, offset); bar = pci_config_read_u32(bdf, offset);
desc.length = (~(bar & 0xfffffff0)) + 1; desc.length = (~(bar & 0xfffffff0)) + 1;

View file

@ -8,14 +8,12 @@ void write_segment_descriptor(uint64_t *entry, uint8_t dpl, uint8_t executable)
| (dpl << 5) | (1 << 4) // S | (dpl << 5) | (1 << 4) // S
| (executable << 3) | (0 << 2) // DC | (executable << 3) | (0 << 2) // DC
| (1 << 1) // RW | (1 << 1) // RW
| (0 << 0) // A | (0 << 0); // A
;
uint8_t flags = (1 << 3) // G uint8_t flags = (1 << 3) // G
| (0 << 2) // DB | (0 << 2) // DB
| (1 << 1) // L | (1 << 1) // L
| (0 << 0) // reserved | (0 << 0); // reserved
;
*entry = 0xffff // limit *entry = 0xffff // limit
| ((uint64_t)0x0000 << 16) // base | ((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)access_byte << 40) // access byte
| ((uint64_t)0xf << 48) // limit | ((uint64_t)0xf << 48) // limit
| ((uint64_t)flags << 52) // flags | ((uint64_t)flags << 52) // flags
| ((uint64_t)0x00 << 56) // base | ((uint64_t)0x00 << 56); // base
;
} }
#define CODE_SEGMENT 1 #define CODE_SEGMENT 1
@ -43,11 +40,12 @@ void init_gdt() {
__asm__("lgdt (%0)" ::"r"(gdtr)); __asm__("lgdt (%0)" ::"r"(gdtr));
__asm__("mov %0, %%ds" ::"r"(DATA_SEGMENT << 3)); __asm__("mov %0, %%ds" ::"r"(DATA_SEGMENT << 3));
// TODO set code segment -> iret?
// TODO set code segment -> iret?
} }
__attribute__ ((packed)) struct __attribute__((packed))
struct int_desc_entry { int_desc_entry {
uint16_t offset1; uint16_t offset1;
uint16_t selector; uint16_t selector;
uint8_t ist; uint8_t ist;
@ -63,8 +61,7 @@ struct int_desc_entry {
void write_segment_selector(uint16_t *ss, uint16_t index) { void write_segment_selector(uint16_t *ss, uint16_t index) {
*ss = (uint16_t)PRIV_KERNEL *ss = (uint16_t)PRIV_KERNEL
| 0 // use GDT | 0 // use GDT
| (index << 3) | (index << 3);
;
} }
#define GATE_TYPE_INTERRUPT 0xe #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); write_segment_selector(&e->selector, CODE_SEGMENT);
e->ist = 0; e->ist = 0;
e->attr = (uint8_t)GATE_TYPE_INTERRUPT e->attr = (uint8_t)GATE_TYPE_INTERRUPT
| (uint8_t)(PRIV_KERNEL << 5) | (uint8_t)(PRIV_KERNEL << 5)
| (uint8_t)(1 << 7) // present | (uint8_t)(1 << 7); // present
;
e->offset2 = (uint16_t) ((offset >> 16) & 0xffff); e->offset2 = (uint16_t) ((offset >> 16) & 0xffff);
e->offset3 = (uint32_t) ((offset >> 32) & 0xffffffff); e->offset3 = (uint32_t) ((offset >> 32) & 0xffffffff);
e->pad = 0; e->pad = 0;