From fb4235163801a1914e939676d3f095da01c95d15 Mon Sep 17 00:00:00 2001 From: Able Date: Mon, 16 Sep 2024 20:45:00 -0500 Subject: [PATCH] minor changes --- kernel/src/arch/x86_64/pci/mod.rs | 22 +++++++++--- sysdata/libraries/pci/src/lib.hb | 18 ++++++---- sysdata/libraries/stn/src/string.hb | 43 ++++++++++++++---------- sysdata/programs/svga_driver/src/main.hb | 6 ++++ 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/kernel/src/arch/x86_64/pci/mod.rs b/kernel/src/arch/x86_64/pci/mod.rs index b02c1834e..872e3a847 100644 --- a/kernel/src/arch/x86_64/pci/mod.rs +++ b/kernel/src/arch/x86_64/pci/mod.rs @@ -47,6 +47,7 @@ pub fn init(device_tree: &mut DeviceTree) { pci_info.set_attribute("id", id); pci_info.set_attribute("device", device_info.device); pci_info.set_attribute("vendor", vendor); + pci_info.set_attribute("bus", bus); pci_info.set_attribute("class", device_info.full_class.to_string()); dev.set_child(pci_info); devices.push((dev_type, dev)); @@ -68,7 +69,8 @@ pub fn check_device(bus: u8, device: u8) -> Option { return None; } - let reg2 = unsafe { pci_config_read(bus, device, 0, 0x8) }; + let (reg2, addr) = unsafe { pci_config_read_2(bus, device, 0, 0x8) }; + log::info!("pci device-({}) addr {} is {}", device, addr, reg2); let class = ((reg2 >> 16) & 0x0000_FFFF) as u16; let pci_class = PciFullClass::from_u16(class); let header_type = get_header_type(bus, device, 0); @@ -459,9 +461,7 @@ unsafe fn pci_config_read(bus: u8, device: u8, func: u8, offset: u8) -> u32 { let func = func as u32; let offset = offset as u32; // construct address param - let address = - ((bus << 16) | (device << 11) | (func << 8) | (offset & 0xFC) | 0x8000_0000) as u32; - + let address = (bus << 16) | (device << 11) | (func << 8) | (offset & 0xFC) | 0x8000_0000; // write address Port::new(0xCF8).write(address); @@ -469,6 +469,20 @@ unsafe fn pci_config_read(bus: u8, device: u8, func: u8, offset: u8) -> u32 { Port::new(0xCFC).read() } +unsafe fn pci_config_read_2(bus: u8, device: u8, func: u8, offset: u8) -> (u32, u32) { + let bus = bus as u32; + let device = device as u32; + let func = func as u32; + let offset = offset as u32; + // construct address param + let address = (bus << 16) | (device << 11) | (func << 8) | (offset & 0xFC) | 0x8000_0000; + // write address + Port::new(0xCF8).write(address); + + // read data + (Port::new(0xCFC).read(), address) +} + unsafe fn pci_config_write(bus: u8, device: u8, func: u8, offset: u8, value: u32) { let bus = bus as u32; let device = device as u32; diff --git a/sysdata/libraries/pci/src/lib.hb b/sysdata/libraries/pci/src/lib.hb index 1445faeff..431f08286 100644 --- a/sysdata/libraries/pci/src/lib.hb +++ b/sysdata/libraries/pci/src/lib.hb @@ -1,3 +1,6 @@ +stn := @use("rel:../../stn/src/lib.hb"); +.{string, memory, buffer} := stn + PCIAddress := struct { bus: u8, device: u8, @@ -13,16 +16,17 @@ scan_bus := fn(): void { config_read32 := fn(bus: u32, device: u32, func: u32, offset: u32): u32 { // construct address param - address := bus << 16 | device << 11 | func << 8 - address |= offset - address &= 0xFC + offset_and := offset & 0xFC + + address := bus << 16 + address |= device << 11 + address |= func << 8 + address |= offset_and address |= 0x80000000 // write address - //Port::new(0xCF8).write(address); + memory.outl(0xCF8, address) // read data - //Port::new(0xCFC).read() - - return + return memory.inl(0xCFC) } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/string.hb b/sysdata/libraries/stn/src/string.hb index e4ba668f6..97bfb9107 100644 --- a/sysdata/libraries/stn/src/string.hb +++ b/sysdata/libraries/stn/src/string.hb @@ -3,36 +3,45 @@ length := fn(ptr: ^u8): int { loop if *(ptr + len) == 0 break else len += 1 return len } - display_int := fn(num: int, p: ^u8): ^u8 { i := 0 if num == 0 { - *p = 48 + *p = 48; + *(p + 1) = 0 return p } + + neg := false + if num < 0 { + neg = true + num = -num + } + loop if num == 0 break else { *(p + i) = num % 10 + 48 num /= 10 i += 1 } - @inline(reverse, p); - *(p + i) = 0 + + if neg { + *(p + i) = 45 + i += 1 + } + + reverse(p, i) + return p } -reverse := fn(s: ^u8): void { - //reverse a string, don't remove digits - len := 0 - loop if *(s + len) == 0 break else len += 1 - i := 0 - j := len - 1 - temp := 0 - loop if i >= j break else { - temp = *(s + i); - *(s + i) = *(s + j); - *(s + j) = temp - i += 1 - j -= 1 +reverse := fn(p: ^u8, len: int): void { + start := 0 + end := len - 1 + loop if start >= end break else { + temp := *(p + start); + *(p + start) = *(p + end); + *(p + end) = temp + start += 1 + end -= 1 } return } \ No newline at end of file diff --git a/sysdata/programs/svga_driver/src/main.hb b/sysdata/programs/svga_driver/src/main.hb index dfd1dd241..f09ed85b4 100644 --- a/sysdata/programs/svga_driver/src/main.hb +++ b/sysdata/programs/svga_driver/src/main.hb @@ -27,6 +27,12 @@ SVGA_disable := fn(): void { } main := fn(): int { + a := pci.config_read32(0, 2, 0, 0x8) + b := "\0\0\0\0\0\0\0" + + string.display_int(a, b) + stn.log.info(b) + init() return 0