setos

拙OS
Log | Files | Refs | LICENSE

commit 58c769c71ff0b5014efee6e64019f15478daffaa
parent 8511061dc757f79b9a660fe821dea1ef84dffb20
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 18 Apr 2024 09:28:37 +0900

adjust endianness.

Diffstat:
Msys/include/libc.h | 3+++
Msys/include/pci.h | 2++
Msys/src/Makefile | 6++++--
Msys/src/kernel/amd64/pci.c | 11++++++-----
Msys/src/kernel/main.c | 5+++--
Msys/src/kernel/pci.c | 1-
Asys/src/libc/endian.c | 19+++++++++++++++++++
7 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/sys/include/libc.h b/sys/include/libc.h @@ -33,6 +33,9 @@ extern Memmap memmap; void *malloc(uintptr); void *memset(void *s, int c, uintptr n); +uint32 endian_little_to_native32(uint32); +uint32 endian_native_to_little32(uint32); + /* typedef char *va_list; // ABI is different from plan9. diff --git a/sys/include/pci.h b/sys/include/pci.h @@ -56,6 +56,7 @@ typedef struct PCIDev { }; uint16 bridge_control; }; + uint8 reg[192]; } PCIDev; #define MaxPCIDev 128 @@ -64,6 +65,7 @@ extern int num_pci_dev; // Pci_config_read16 reads pci header. // The least significant bit of offset is ignored. +// The endianness is adjusted to the machine's endianness from pci's little-endian. uint16 pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset); uint32 pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset); uint16 pci_config_read_vendor_id(uint8 bus, uint8 dev, uint8 func); diff --git a/sys/src/Makefile b/sys/src/Makefile @@ -64,12 +64,14 @@ kernel/$(ARCH)/machine.o: kernel/$(ARCH)/machine.s kernel/xhc.o: kernel/xhc.c ../include/xhc.h $(CC) $(CFLAGS) -c -o $@ kernel/xhc.c -../lib/libc.a: libc/strcpy.o libc/memset.o ../lib - ar rcs $@ libc/strcpy.o libc/memset.o +../lib/libc.a: libc/strcpy.o libc/memset.o libc/endian.o ../lib + ar rcs $@ libc/strcpy.o libc/memset.o libc/endian.o libc/strcpy.o: libc/strcpy.c ../include/libc.h $(CC) $(CFLAGS) -c -o $@ libc/strcpy.c libc/memset.o: libc/memset.c ../include/libc.h $(CC) $(CFLAGS) -c -o $@ libc/memset.c +libc/endian.o: libc/endian.c ../include/libc.h + $(CC) $(CFLAGS) -c -o $@ libc/endian.c ../lib: mkdir -p ../lib diff --git a/sys/src/kernel/amd64/pci.c b/sys/src/kernel/amd64/pci.c @@ -12,14 +12,15 @@ const uint16 pci_config_data = 0xcfc; uint16 pci_config_read16(uint8 bus, uint8 dev, uint8 func, uint8 offset) { - uint32 addr; + uint32 addr, tmp; addr = 1 << 31 | // enable bit. (uint32) bus << 16 | (uint32) (dev&0x1f) << 11 | (uint32) (func&3) << 8 | (offset&0xfc); // the least 2 bits should be 0. - io_out32(pci_config_address, addr); - return (io_in32(pci_config_data) >> ((offset&2)*8)) & 0xffff; + io_out32(pci_config_address, endian_native_to_little32(addr)); + tmp = endian_little_to_native32(io_in32(pci_config_data)); + return (tmp >> (offset&2)*8) & 0xffff; } uint32 @@ -31,6 +32,6 @@ pci_config_read32(uint8 bus, uint8 dev, uint8 func, uint8 offset) (uint32) (dev&0x1f) << 11 | (uint32) (func&3) << 8 | (offset&0xfc); // the least 2 bits should be 0. - io_out32(pci_config_address, addr); - return io_in32(pci_config_data); + io_out32(pci_config_address, endian_native_to_little32(addr)); + return endian_little_to_native32(io_in32(pci_config_data)); } diff --git a/sys/src/kernel/main.c b/sys/src/kernel/main.c @@ -47,9 +47,9 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) m.pos.x = (m.pos.x + 1) % root_window.size.x; draw_mouse(&root_window, &m); - cons_printf(&con0, "hello world!\n"); - cons_printf(&con0, "pixel at {%d, %d}: %x\n", p.x, p.y, get_pixel(&root_window, p)); pci_scan_all_bus(); + + cons_printf(&con0, "sizeof(PCIDev) = %d\n", sizeof(PCIDev)); cons_printf(&con0, "pci devices:\n"); for (int i = 0; i < num_pci_dev; i++) { cons_printf(&con0, "%d:\t%x\t%x\t%x\t%x\t%x\n", i, @@ -72,6 +72,7 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) cons_printf(&con0, "HCSPARAMS1: %x\n", xhccr->HCSPARAMS1); cons_printf(&con0, "USBCMD: %x\n", xhcor->USBCMD); cons_printf(&con0, "USBSTS: %x\n", xhcor->USBSTS); + cons_printf(&con0, "FLADJ: %x\n", pci_dev[i].reg[0x21]); } } diff --git a/sys/src/kernel/pci.c b/sys/src/kernel/pci.c @@ -69,7 +69,6 @@ pci_scan_func(uint8 bus, uint8 dev, uint8 func) } // populate the PCIDev struct. for (i = 0; i < (sizeof(PCIDev) + 3) / 4; i++) { - // TODO: endianness? ((uint32 *)&pci_dev[num_pci_dev])[i] = pci_config_read32(bus, dev, func, i * 4); } diff --git a/sys/src/libc/endian.c b/sys/src/libc/endian.c @@ -0,0 +1,19 @@ +#include <libc.h> + +uint32 +endian_little_to_native32(uint32 n) +{ + uint8 *m = (uint8 *) &n; + return (uint32) m[0] | (uint32) m[1] << 8 | (uint32) m[2] << 16 | (uint32) m[3] << 24; +} + +uint32 +endian_native_to_little32(uint32 n) +{ + uint8 m[4]; + m[0] = (uint8) n&0x000000ff; + m[1] = (uint8) ((n&0x0000ff00) >> 8); + m[2] = (uint8) ((n&0x00ff0000) >> 16); + m[3] = (uint8) ((n&0xff000000) >> 24); + return *(uint32 *)m; +}