Bladeren bron

8.990u-mods: ahci's not working. drunk, do it later

rain 5 jaren geleden
bovenliggende
commit
0f2f506d5b
18 gewijzigde bestanden met toevoegingen van 601 en 84 verwijderingen
  1. 1 0
      LINES
  2. 4 8
      Makefile
  3. 3 0
      README.md
  4. 2 0
      include/arch/x86/paging.h
  5. 2 2
      include/kernel/init.h
  6. 2 0
      include/kernel/mm.h
  7. 47 10
      include/kernel/pci.h
  8. 17 17
      include/kernel/pci_vendors.h
  9. 241 0
      include/modules/ahci.h
  10. 3 7
      init/main.c
  11. 4 2
      kernel/int.c
  12. 31 6
      kernel/mm.c
  13. 39 5
      kernel/pci.c
  14. 0 4
      kernel/rlib/fifo.c
  15. 0 12
      kernel/sched.c
  16. 1 1
      modules/Makefile
  17. 204 0
      modules/ahci.c
  18. 0 10
      user/Makefile

+ 1 - 0
LINES

@@ -36,4 +36,5 @@
 2021-8-14 8.153: 8153 lines, 54 files, rain
 2021-8-14 8.275: 8229 lines, 55 files, rain
 2021-8-15 8.413: 8413 lines, 57 files, rain
+2021-8-17 8.990: 8990 lines, 59 files, rain
 

+ 4 - 8
Makefile

@@ -3,12 +3,10 @@ BOOT_SRCS = boot/boot.asm boot/loader.asm
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c kernel/rlib/fifo.c 
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o kernel/rlib/fifo.o 
-MODULE_SRCS = 
-MODULE_OBJS = 
-USER_SRCS = user/hello.c
-USER_OBJS = user/hello.o 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o kernel/tty.o kernel/console.o $(RLIB_OBJS) $(MODULE_OBJS) $(USER_OBJS) 
-KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c kernel/tty.c kernel/console.c $(RLIB_SRCS) $(MODULE_SRCS) $(USER_SRCS)
+MODULE_SRCS = modules/ahci.c 
+MODULE_OBJS = modules/ahci.o 
+KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o kernel/tty.o kernel/console.o $(RLIB_OBJS) $(MODULE_OBJS) 
+KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c kernel/tty.c kernel/console.c $(RLIB_SRCS) $(MODULE_SRCS) 
 
 all: Makefile
 	make -C tools/
@@ -17,7 +15,6 @@ all: Makefile
 	make -C modules/
 	make -C kernel/ 
 	make -C kernel/rlib 
-	make -C user/ 
 	make xnix.img
 	make vmxnix
 
@@ -45,7 +42,6 @@ clear:
 	make -C boot/ clear
 	make -C init/ clear
 	make -C kernel/ clear
-	make -C user/ clear
 	make -C tools/ clear
 	rm xnix.img kbinary kernel.o vmxnix 
 	make -C modules/ clear

+ 3 - 0
README.md

@@ -148,3 +148,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;move the `tty.c` from module, to the main kernel because we need it, and 
 &nbsp;&nbsp;&nbsp;&nbsp;it's not a device. 
 
+8.990u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working. drunk, do it later. 
+

+ 2 - 0
include/arch/x86/paging.h

@@ -52,6 +52,8 @@
 /* 0x07 */
 #define PATT_USER   (PAGE_PRESENT | PAGE_RDWR | PAGE_USER | PAGE_PAT)
 
+#define PATT_DEVMM  (PAGE_PRESENT | PAGE_RDWR | PAGE_PAT | PAGE_NOCACHE)
+
 #define PAGE_TABLE_BASE (0x0000000000000000)
 
 #endif

+ 2 - 2
include/kernel/init.h

@@ -34,14 +34,14 @@
 
 /* fastest, but no syslog and schk */
 
-// #define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
+#define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
 
 /* standard */
 
 // #define DEBUG_LEVEL		DEV_DEBUG_LEVEL
 
 /* slowest: schk will display function info in this mode */
-#define DEBUG_LEVEL		TEST_DEBUG_LEVEL
+// #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
 #include <kernel/console.h>
 

+ 2 - 0
include/kernel/mm.h

@@ -135,6 +135,8 @@ struct mempool {
 
 extern struct mempool pool;
 extern struct mempool upool;
+extern struct mempool devpool;
+
 
 void *kmalloc(struct mempool *, __uint64_t);
 void kfree(struct mempool *, void *, __uint64_t);

+ 47 - 10
include/kernel/pci.h

@@ -36,15 +36,6 @@ __uint32_t pci_lspci(void);
 void pci_infodev(__uint8_t, __uint8_t, __uint8_t);
 
 
-struct pci_device {
-	__uint16_t devid;
-	__uint16_t vendor;
-
-	char *devname;
-
-	/* the device configure word (256 bytes) */
-	char *config;
-};
 
 /* the general pci header */
 
@@ -56,7 +47,7 @@ struct pci_header {
 	__uint16_t status;
 
 	__uint8_t revision;
-	__uint8_t prof_if;
+	__uint8_t prog_if;
 
 	__uint8_t subclass;
 	__uint8_t class_code;
@@ -176,6 +167,52 @@ struct pci_header2 {
 	__uint32_t lagency_base;
 } __attribute__ ((packed));
 
+struct pci_device {
+	__uint16_t devid;
+	__uint16_t vendor;
+
+	char *devname;
+
+	/* the device configure word (256 bytes) */
+	char *config;
+
+	errno_t (* dev_init) (struct pci_header0 *);
+};
+
+
+struct pci_vendor_table {
+	__uint8_t class;
+	__uint8_t subclass;
+
+	errno_t (* dev_init) (struct pci_header0 *);
+};
+
+
+/* pci_header.class-codes */
+
+#define CLASS_UNK 	0x00
+#define CLASS_DISK 	0x01
+#define CLASS_NETCTL 	0x02
+#define CLASS_DISPLAY 	0x03
+#define CLASS_MEDIACTL 	0x04
+#define CLASS_MMCTL 	0x05
+#define CLASS_BRIDGE 	0x06
+#define CLASS_CMMCTL 	0x07
+#define CLASS_SYSDEV 	0x08
+#define CLASS_INPUTDEV 	0x09
+#define CLASS_DOCK 	0x0a
+#define CLASS_CPU 	0x0b
+#define CLASS_SBUS_CTL	0x0c
+#define CLASS_WIRELESS 	0x0d
+#define CLASS_INTCTL 	0x0e
+#define CLASS_SCMCTL 	0x0f
+#define CLASS_ENCRY 	0x10
+#define CLASS_SIGPROC 	0x11
+#define CLASS_PROCSPD 	0x12
+#define CLASS_NEACC 	0x13
+#define CLASS_FPU 	0x40
+#define CLASS_NOCLASS 	0xff
+
 
 #endif
 

+ 17 - 17
user/hello.c → include/kernel/pci_vendors.h

@@ -23,30 +23,30 @@
 
 
 
-#include <sys/syscall.h>
 
+#ifdef _PCIVEND_H
 
-static char msg[] = "hello, world";
+#error "pci_vendor.h includes for the second time. "
 
+#endif
 
-__uint64_t utask_a(__uint64_t arg) {
-	__uint64_t addr;
+#define _PCIVEND_H
 
-	/* we must use this, because &msg will returns the 
-	 * msg(%rip) (0x800000 based), but msg is in the 
-	 * .data section. 
-	 * */
+#include <kernel/types.h>
+#include <kernel/errno.h>
 
-	__asm__ __volatile__ (
-		"leaq msg, %0\n\t"
-		: "=r" (addr)
-	);
+#include <kernel/pci.h>
 
-	do_call_1(2, addr);
-	do_call_0(1);
+/* (only built-in devices) */
+/* register your own device's dev_init to here */
 
-	/* it won't here */
-	for (;;);
-}
+#define foo(fn) extern errno_t fn (struct pci_header0 *)
 
+foo(ahci_init);
+
+
+struct pci_vendor_table pci_vendors[] = {
+	{CLASS_DISK, 0x06, ahci_init}, 
+	{0xff, 0xff, NULL}
+};
 

+ 241 - 0
include/modules/ahci.h

@@ -0,0 +1,241 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+
+struct ahci_hba_portctl {
+	__uint32_t cmd_listl;
+	__uint32_t cmd_listh;
+
+	__uint32_t fisl;
+	__uint32_t fish;
+
+	__uint32_t int_status;
+	__uint32_t int_enable;
+
+	__uint32_t cmd;
+	__uint32_t rsv0;
+	__uint32_t tfd;
+
+#define SIG_ATA 	0x00000101
+#define SIG_ATAPI 	0xeb140101
+#define SIG_SEMB 	0xc33c0101
+#define SIG_PORTMUL 	0x96690101
+
+	__uint32_t sig;
+
+	__uint32_t status;
+	__uint32_t ctl;
+	__uint32_t error;
+
+	__uint32_t active;
+
+	__uint32_t ci;
+	__uint32_t sntf;
+	__uint32_t fbs;
+	__uint32_t rsv1[11];
+	__uint32_t vendor[4];
+} __attribute__ ((packed));
+
+
+/* the pcihdr.bar5 points to this */
+
+struct ahci_hba {
+	/* general host control */
+
+	__uint32_t cap0;
+	__uint32_t ghc;
+	__uint32_t int_status;
+	__uint32_t implted_ports;
+	__uint32_t version;
+	__uint32_t ccc_ctl, ccc_pts;
+	__uint32_t em_loc, em_ctl;
+	__uint32_t cap1;
+	__uint32_t bohc;
+
+	char rsvd[0x74];
+
+	/* 1-31 is optional */
+	struct ahci_hba_portctl portctl[32];
+} __attribute__ ((packed));
+
+extern volatile struct ahci_hba *hba;
+
+#define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
+
+
+#define FIS_HOSTTODEV	0x27
+#define FIS_DEVTOHOST 	0x34
+#define FIS_DMA_ACT 	0x39
+#define FIS_DMA_SETUP 	0x41
+#define FIS_DATA 	0x46
+#define FIS_BIST 	0x58
+#define FIS_PIO_SETUP 	0x5f
+#define FIS_DEV_BITS 	0xa1
+
+#define DEV_NODEV 	0x00
+#define DEV_SATA 	0x01
+#define DEV_ATAPI 	0x02
+#define DEV_PORTMUL 	0x03
+#define DEV_SEMB 	0x04
+
+
+struct fis_h2d {
+	__uint8_t fis_type;
+	__uint8_t pmport : 4;
+	__uint8_t rsvd0 : 3;
+	/* cmd / ctrl */
+	__uint8_t cmd : 1;
+	__uint8_t command;
+	__uint8_t feature_low;
+
+	__uint8_t lba0, lba1, lba2;
+	__uint8_t dev;
+
+	__uint8_t lba3, lba4, lba5;
+	__uint8_t feature_high;
+
+	__uint8_t count_low;
+	__uint8_t count_high;
+	__uint8_t icc;
+	__uint8_t ctl;
+
+	__uint32_t rsvd1;
+} __attribute__ ((packed));
+
+struct fis_d2h {
+	__uint8_t fis_type;
+
+	__uint8_t pmport : 4;
+	__uint8_t rsvd0 : 2;
+	__uint8_t irq : 1;
+	__uint8_t rsvd1 : 1;
+
+	__uint8_t status;
+	__uint8_t error;
+
+	__uint8_t lba0, lba1, lba2;
+	__uint8_t dev;
+
+	__uint8_t lba3, lba4, lba5;
+	__uint8_t rsvd2;
+
+	__uint8_t count_low, count_high;
+	__uint16_t rsvd3;
+
+	__uint32_t rsvd4;
+} __attribute__ ((packed));
+
+struct fis_data {
+	__uint8_t fis_type;
+	__uint8_t pmport : 4;
+	__uint8_t rsvd0 : 4;
+
+	__uint16_t rsvd1;
+
+	__uint32_t data[0];
+} __attribute__ ((packed));
+
+
+/* the command slot (in the command list) */
+
+struct cmd_header {
+	/* fis length in __uint32_t */
+	__uint8_t fis_len : 5;
+	__uint8_t atapi : 1;
+	__uint8_t wr : 1;
+	__uint8_t pre : 1;
+
+	__uint8_t rst : 1;
+	__uint8_t bist : 1;
+	__uint8_t clear : 1;
+	__uint8_t rsvd0 : 1;
+	__uint8_t pmport : 4;
+
+	/* prdt length (in entries) */
+	__uint16_t prdt_len;
+
+	__uint32_t bytes_transm;
+
+	__uint32_t cmd_table_low;
+	__uint32_t cmd_table_high;
+
+	__uint32_t rsvd1[4];
+} __attribute__ ((packed));
+
+
+struct prdt_entry {
+	__uint32_t buf_low;
+	__uint32_t buf_high;
+
+	__uint32_t rsvd0;
+
+	__uint32_t nbytes : 22;
+	__uint32_t rsvd1 : 9;
+	__uint32_t irq : 1;
+} __attribute__ ((packed));
+
+
+/* this is points from the cmd_header */
+
+struct cmd_table {
+	__uint8_t cmd_fis[64];
+	__uint8_t atapi_cmd[16];
+	__uint8_t rsvd0[48];
+
+	/* max 65536 */
+	struct prdt_entry prdts[65536];
+} __attribute__ ((packed));
+
+
+void do_ahci_intr(void);
+void set_base(volatile struct ahci_hba_portctl *, int);
+void ahci_read(volatile struct ahci_hba_portctl *, __uint64_t, __uint16_t, char *);
+
+__uint64_t ahci_buffer;
+
+
+#define HBA_PORT_CMD_ST 	0x0001
+#define HBA_PORT_CMD_FRE 	0x0010
+#define HBA_PORT_CMD_FR 	0x4000
+#define HBA_PORT_CMD_CR 	0x8000
+
+#define ATACMD_RD 	0x20
+#define ATACMD_RD48		0x24
+
+#define ATACMD_WR 	0x30
+#define ATACMD_WR48	0x34
+
+#define ATACMD_RDDMA 	0xc8
+#define ATACMD_RDDMA48 	0x25
+
+#define ATACMD_WRDMA 	0xca
+#define ATACMD_WRDMA48 	0x35
+
+#define ATACMD_ID	0xec
+

+ 3 - 7
init/main.c

@@ -70,11 +70,12 @@ int init(void) {
 	int_init();
 	mm_init();
 
-	sti();
-
 	tty_read(&stdin, buf, 256);
 
 	task_init();
+	kbd_init();
+
+	sti();
 
 	printk(white, "total %d devices. \n", pci_lspci());
 
@@ -86,11 +87,6 @@ int init(void) {
 	else 
 		printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
 
-	pci_infodev(0, 0x1f, 2);
-
-	for (;;) 
-		put_keycode();
-
 	return 0;
 }
 

+ 4 - 2
kernel/int.c

@@ -136,7 +136,7 @@ void die(const char *s, const __uint64_t *rsp, char nr) {
 
 	printk(red, "rsp now: %q\n", rsp);
 
-	printk(red, "rax\t\t\trcx\t\t\trdx\n");
+/*	printk(red, "rax\t\t\trcx\t\t\trdx\n");
 	printk(red, "%q\t%q\t%q\n", rsp[off_rax], rsp[off_rcx], rsp[off_rdx]);
 
 	printk(red, "rbx\t\t\trbp\t\t\trsp\n");
@@ -156,8 +156,10 @@ void die(const char *s, const __uint64_t *rsp, char nr) {
 
 	printk(red, "ds\t\t\tss\n");
 	printk(red, "%w\t%w\n", rsp[off_ds] & 0xffff, rsp[off_ss] & 0xffff);	
+*/
 
-	printk(red, "instruction: %q\n", *(__uint64_t *) rsp[off_rip]);
+	printk(red, "rip: %q  instruction: %q\n", rsp[off_rip], *(__uint64_t *) rsp[off_rip]);
+	printk(red, "ds: %w\n", rsp[off_ds] & 0xffff);
 
 	trace_mm();
 

+ 31 - 6
kernel/mm.c

@@ -45,7 +45,7 @@
 
 struct sysmm *mm;
 
-struct mempool pool, upool;
+struct mempool pool, upool, devpool;
 
 
 void mm_init(void) {
@@ -163,6 +163,9 @@ void mm_init(void) {
 
 	pool.page = alloc_page();
 
+	if (!pool.page) 
+		panic("mm_init: fatal allocate page for pool. \n");
+
 	upool.size = 0x200000;
 	upool.used = 0;
 
@@ -177,8 +180,29 @@ void mm_init(void) {
 
 	upool.page = alloc_page_att(PATT_USER);
 
-	upool.next = NULL;
+	if (!upool.page) 
+		panic("mm_init: fatal allocate page for upool. ");
+
 	pool.next = &upool;
+	
+	devpool.size = 0x200000;
+	devpool.used = 0;
+
+	devpool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
+
+	devpool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
+
+	if (!devpool.bitmap) 
+		panic("mm_init: fatal allocate space for devpool. ");
+
+	memset(devpool.bitmap, 0, 0x200000 / 64 / 8);
+
+	devpool.page = alloc_page_att(PATT_DEVMM);
+
+	if (!devpool.page) 
+		panic("mm_init: fatal allocate page for devpool. ");
+
+	upool.next = &devpool;
 
 	return;
 
@@ -187,8 +211,9 @@ void mm_init(void) {
 
 /* find an empty page table entry, and return its number */
 
-void *mapping(void *addr, __uint64_t attr) {
+void *mapping(void *p, __uint64_t attr) {
 	__uint64_t i;
+	void *addr = (void *) ((__uint64_t) p & PAGE_MASK);
 
 	/* we just use the (max) 1 GB memory in kernel (512 pages) */
 
@@ -198,7 +223,7 @@ void *mapping(void *addr, __uint64_t attr) {
 		if (!PT_ADDR[i]) {
 			PT_ADDR[i] = (__uint64_t) addr | attr;
 			invalidate();
-			return (void *) (i << PAGE_SHIFT);
+			return (void *) ((i << PAGE_SHIFT) + p - addr);
 		}
 	}
 
@@ -256,7 +281,7 @@ void *kmalloc(struct mempool *pool, __uint64_t size) {
 	if (points == 1) {
 		for (i = 0; bitmap_read(pool->bitmap, i); i++) {
 			if (i > pool->entries) 
-				return NULL;
+				goto panic;
 		}
 
 		bitmap_set(pool->bitmap, i, 1);
@@ -331,7 +356,7 @@ void trace_mm(void) {
 
 	for (i = 0; i < 512; i++) {
 		if (pt[i] & ~0xff) 
-			printk(yellow, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
+			printk(yellow, "%q -> %q: %b\n", i << PAGE_SHIFT, pt[i] & ~0xff, pt[i] & 0xff);
 	}
 		
 

+ 39 - 5
kernel/pci.c

@@ -30,6 +30,8 @@
 
 #include <kernel/int.h>
 #include <kernel/pci.h>
+#include <kernel/pci_vendors.h>
+
 #include <lib/string.h>
 
 #include <arch/x86/int.h>
@@ -61,8 +63,13 @@ __uint32_t pci_lspci(void) {
 			for (func = 0; func < 8; func++) {
 				if (pci_read_config(bus, slot, func, 0) != 0xffffffff) {
 					/* exists */
-					printk(yellow, "pci: %b:%b:%b exists: %x. \n", bus, slot, func, pci_read_config(bus, slot, func, 0));
+					printk(yellow, "pci: found %b:%b:%b %w %x. \n", 
+						bus, slot, func, 
+						pci_read_config(bus, slot, func, 8) >> 16, 
+						pci_read_config(bus, slot, func, 0));
 					devs++;
+
+					pci_infodev(bus, slot, func);
 				}
 			}
 		}
@@ -83,7 +90,7 @@ void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
 	memcpy((char *) &header, (char *) buf, sizeof(struct pci_header));
 
 	/* put the info of this device on the screen */
-	printk(yellow, "vendor id: %w\n", header.vendor);
+/*	printk(yellow, "vendor id: %w\n", header.vendor);
 	printk(yellow, "device id: %w\n", header.dev);
 
 	printk(yellow, "revision: %b\n", header.revision);
@@ -94,11 +101,11 @@ void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
 	printk(yellow, "latency: %b\n", header.latency);
 
 	printk(yellow, "header type: %b\n", header.hdr_type);
+*/
 
-	if (header.hdr_type == 0) {
-		struct pci_header0 *hdr = (struct pci_header0 *) buf;
+	struct pci_header0 *hdr = (struct pci_header0 *) buf;
 
-		printk(yellow, "bar0: %x  ", hdr->bar0);
+/*		printk(yellow, "bar0: %x  ", hdr->bar0);
 		printk(yellow, "bar1: %x\n", hdr->bar1);
 
 		printk(yellow, "bar2: %x  ", hdr->bar2);
@@ -108,7 +115,34 @@ void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
 		printk(yellow, "bar5: %x\n", hdr->bar5);
 
 		printk(yellow, "irq: %b\n", hdr->irqnr);
+*/
+
+	/* search in the vendor-dev table, and got the dev_init */
+	
+	
+	errno_t (* dev_init) (struct pci_header0 *) = NULL;
+
+	for (i = 0; ; i++) {
+		if (pci_vendors[i].class == 0xff) 
+			break;
+
+		if (pci_vendors[i].class == header.class_code && 
+			pci_vendors[i].subclass == header.subclass) {
+			dev_init = pci_vendors[i].dev_init;
+			break;
+		}
+	}
+
+	if (!dev_init) {
+		printk(red, "module not found for vendor %w, device %w. \n", 
+				header.vendor, header.dev);
+		return;
 	}
 
+	printk(yellow, "dev_init: %q\n", dev_init);
+
+	dev_init(hdr);
+
 	return;
 }
+

+ 0 - 4
kernel/rlib/fifo.c

@@ -68,15 +68,11 @@ __fifo_buf_t fifo_get(struct fifo *fifo) {
 
 
 void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
-	schk_init();
-
 	fifo->buf[fifo->next_wr++] = value;
 	fifo->need_read++;
 
 	if (fifo->next_wr == fifo->total) 
 		fifo->next_wr = 0;
 
-	schk_leave();
-
 	return;
 }

+ 0 - 12
kernel/sched.c

@@ -81,18 +81,6 @@ int task_init(void) {
 
 	current = &init_task;
 
-	/* this page (what place what we are) is k-only, so allocate a
-	 * new page for user. 
-	 * */
-
-	void *p = alloc_page_att(PATT_USER);
-	memcpy(p, (char *) utask_a, 1024);
-
-	printk(white, "new page for utask_a: %q\n", p);
-
-	utask_create(p, 0x12345678);
-	switch_to(tasks[0]);
-
 	return 0;
 }
 

+ 1 - 1
modules/Makefile

@@ -1,4 +1,4 @@
-OBJS = 
+OBJS = ahci.o 
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 all: $(OBJS)

+ 204 - 0
modules/ahci.c

@@ -0,0 +1,204 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/console.h>
+#include <kernel/pci.h>
+#include <kernel/mm.h>
+
+#include <kernel/int.h>
+#include <kernel/panic.h>
+
+#include <arch/x86/paging.h>
+#include <modules/ahci.h>
+#include <lib/string.h>
+
+
+volatile struct ahci_hba *hba;
+__uint64_t ahci_buffer;
+
+build_irq(0x2a);
+
+
+static char buf[512] = {0};
+
+/* calls from pci_init() */
+
+errno_t ahci_init(struct pci_header0 *hdr) {
+	ahci_info("bar0: %q\n", hdr->bar0);
+	ahci_info("bar1: %q\n", hdr->bar1);
+	ahci_info("bar2: %q\n", hdr->bar2);
+	ahci_info("bar3: %q\n", hdr->bar3);
+	ahci_info("bar4: %q\n", hdr->bar4);
+	ahci_info("bar5: %q\n", hdr->bar5);
+
+	ahci_info("irq: %b\n", hdr->irqnr);
+
+	/* map the bar5 to memory */
+	hba = mapping((void *) (__uint64_t) hdr->bar5, PATT_DEVMM);
+
+	ahci_info("hba mem maps to the %q. \n", hba);
+
+	/* ahci enable, interrupt enable, hba reset */
+	hba->ghc = 0x8000000;
+
+	register_irq(0x20 + 0x0a, irq0x2a_interrupt, do_ahci_intr);
+
+	struct cmd_header *cmdhdr = kmalloc(&devpool, sizeof(struct cmd_header));
+
+	if (!cmdhdr) 
+		panic("ahci_init: failed to allocate space for ahci command header. \n");
+
+	hba->portctl[0].cmd_listl = (__uint32_t) ((__uint64_t) cmdhdr) & 0xffffffff;
+	hba->portctl[0].cmd_listh = (__uint32_t) ((__uint64_t) cmdhdr >> 32) & 0xffffffff;
+
+	ahci_info("current ahci list address: %q. \n", (__uint64_t) hba->portctl[0].cmd_listl);
+
+	struct cmd_table *cmdtbl = alloc_page_att(PATT_DEVMM);
+
+	((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
+		->cmd_table_low = (__uint32_t) ((__uint64_t) cmdtbl) & 0xffffffff;
+
+
+	((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
+		->cmd_table_high = (__uint32_t) ((__uint64_t) cmdtbl >> 32) & 0xffffffff;
+
+	ahci_info("current ahci command table address: %q. \n", 
+			(__uint64_t) ((struct cmd_header *) (__uint64_t) hba->portctl[0].cmd_listl)
+			->cmd_table_low);
+
+	ahci_info("reading the sector 1 from the ahci disk...\n");
+
+	ahci_read(hba->portctl, 0x0000000000000001, 1, buf);
+
+	return 0;
+}
+
+
+void do_ahci_intr(void) {
+	ahci_info("irq 0x0e: sata master disk. \n");
+
+	ahci_info("disk data: \n");
+
+	int i;
+
+	for (i = 0; i < 512 >> 3; i++) 
+		ahci_info("%q", ((__uint64_t *) buf)[i]);
+
+	return;
+}
+
+
+static int find_free_cmdslot(volatile struct ahci_hba_portctl *port) {
+	int i;
+	__uint32_t slot = (port->active | port->ci);
+
+	for (i = 0; i < 32; i++) {
+		if (!(slot & 1)) 
+		       return i;
+
+		slot >>= 1;	
+	}
+
+	ahci_info("find_free_cmdslot: warning: can't find a free slot");
+
+	return 0;
+}
+
+
+void ahci_read(volatile struct ahci_hba_portctl *port, __uint64_t lba, __uint16_t nsectors, char *buf) {
+	int slot = find_free_cmdslot(port);
+	
+	ahci_info("ahci_read: free cmd slot: %d. \n", slot);
+
+	port->int_status = -1;
+
+	struct cmd_header *cmdhdr = (struct cmd_header *) (__uint64_t) port->cmd_listl;
+	ahci_info("ahci_read: command header address: %q. \n", cmdhdr);
+
+	cmdhdr += slot;
+
+	cmdhdr->fis_len = sizeof(struct fis_h2d) / sizeof(int);
+	cmdhdr->wr = 0;
+	cmdhdr->atapi = 0;
+	cmdhdr->clear = 1;
+	cmdhdr->pre = 1;
+	/* the max size of a prdt is 8kb (16 sectors, >> 4) */
+	cmdhdr->prdt_len = ((nsectors - 1) >> 4) + 1;
+
+	struct cmd_table *cmdtbl = (struct cmd_table *) (__uint64_t) cmdhdr->cmd_table_low;
+	ahci_info("ahci_read: command table address: %q. \n", cmdtbl);
+
+	int i;
+
+	/* setup first 1 --- (last-1), and setup the last one later */
+
+	for (i = 0; i < cmdhdr->prdt_len - 1; i++) {
+		memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
+
+		cmdtbl->prdts[i].buf_low = (__uint32_t) (__uint64_t) buf & 0xffffffff;
+		cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
+		/* max 8kb */
+		cmdtbl->prdts[i].nbytes = 8191;
+		cmdtbl->prdts[i].irq = 1;
+
+		buf += 8192;
+	}
+
+	memset(cmdtbl->prdts + i, 0x00, sizeof(struct prdt_entry));
+
+	cmdtbl->prdts[i].buf_low = (__uint32_t) ((__uint64_t) buf) & 0xffffffff;
+	cmdtbl->prdts[i].buf_high = (__uint32_t) ((__uint64_t) buf >> 32) & 0xffffffff;
+	cmdtbl->prdts[i].nbytes = (nsectors << 9) % 8192 - 1;
+	cmdtbl->prdts[i].irq = 1;
+
+	struct fis_h2d *h2dfis = (struct fis_h2d *) &cmdtbl->cmd_fis;
+
+	h2dfis->fis_type = FIS_HOSTTODEV;
+	h2dfis->cmd = 1;
+	h2dfis->command = ATACMD_RDDMA;
+
+	h2dfis->lba0 = lba & 0xff;
+	h2dfis->lba1 = (lba >> 8) & 0xff;
+	h2dfis->lba2 = (lba >> 16) & 0xff;
+	h2dfis->lba3 = (lba >> 24) & 0xff;
+	h2dfis->lba4 = (lba >> 32) & 0xff;
+	h2dfis->lba5 = (lba >> 40) & 0xff;
+
+	h2dfis->count_low = nsectors & 0xff;
+	h2dfis->count_high = (nsectors >> 8) & 0xff;
+
+	h2dfis->dev = 0xe0;
+
+	port->active = 1;
+	port->cmd |= 0x10000003;
+	port->ci = 1;
+
+	return;
+}
+

+ 0 - 10
user/Makefile

@@ -1,10 +0,0 @@
-CFLAGS = -fno-builtin -m64 -I ../include/ -Wall -Werror -g -Wno-unused-variable
-
-all: hello.o
-
-%.o: %.c
-	gcc -c $*.c -o $*.o $(CFLAGS)
-
-clear:
-	rm *.o 
-