فهرست منبع

9.389u-mods: ahci driver is not woking........................

rain 5 سال پیش
والد
کامیت
7ab20bee59
23فایلهای تغییر یافته به همراه413 افزوده شده و 106 حذف شده
  1. 1 0
      LINES
  2. 2 0
      README.md
  3. 1 1
      include/arch/x86/paging.h
  4. 0 1
      include/kernel/console.h
  5. 5 2
      include/kernel/init.h
  6. 2 0
      include/kernel/int.h
  7. 6 2
      include/kernel/mm.h
  8. 3 1
      include/kernel/panic.h
  9. 1 1
      include/kernel/schk.h
  10. 23 3
      include/modules/ahci.h
  11. 15 4
      init/main.c
  12. 9 24
      kernel/console.c
  13. 6 1
      kernel/int.c
  14. 25 1
      kernel/irq.c
  15. 8 0
      kernel/keyboard.c
  16. 51 3
      kernel/mm.c
  17. 58 2
      kernel/panic.c
  18. 11 0
      kernel/pci.c
  19. 47 5
      kernel/sched.c
  20. 10 0
      kernel/sys.c
  21. 10 0
      kernel/tty.c
  22. 0 8
      kernel/vsprintf.c
  23. 119 47
      modules/ahci.c

+ 1 - 0
LINES

@@ -39,4 +39,5 @@
 2021-8-17 8.990: 8990 lines, 59 files, rain
 2021-8-18 9.106: 9106 lines, 59 files, rain
 2021-8-19 9.085: 9085 lines, 59 files, rain
+2021-8-21 9.389: 9389 lines, 59 files, rain
 

+ 2 - 0
README.md

@@ -158,3 +158,5 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 9.085u-mods:  
 &nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working... drunk, i'll not fix it. 
 
+9.389u-mods:  
+&nbsp;&nbsp;&nbsp;&nbsp;ahci driver is not working............

+ 1 - 1
include/arch/x86/paging.h

@@ -52,7 +52,7 @@
 /* 0x07 */
 #define PATT_USER 	(PAGE_PRESENT | PAGE_RDWR | PAGE_USER | PAGE_PAT)
 
-#define PATT_DEVMM 	(PAGE_PRESENT | PAGE_RDWR | PAGE_PAT | PAGE_WTHROUGH)
+#define PATT_DEVMM 	(PAGE_PRESENT | PAGE_RDWR | PAGE_WTHROUGH | PAGE_CACHEDIS | PAGE_PAT)
 
 #define PAGE_TABLE_BASE 0
 

+ 0 - 1
include/kernel/console.h

@@ -40,7 +40,6 @@ void putc(char, __uint8_t);
 void print(__uint8_t, char *);
 
 int printk(__uint8_t, char *, ...);
-void syslog(const char *);
 
 void update_cursor(void);
 __uint16_t read_cursor(void);

+ 5 - 2
include/kernel/init.h

@@ -34,14 +34,17 @@
 
 /* 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
+
+/* how many we should put on the screen (functions name) */
+#define TRACE_SYSLOG_NR 	8
 
 #include <kernel/console.h>
 

+ 2 - 0
include/kernel/int.h

@@ -68,6 +68,8 @@ void do_irq(const __uint64_t *, __uint64_t);
 void do_timer(void);
 void do_keyboard(void);
 
+void trace_irq(void);
+
 
 /* kernel/timer.S */
 extern __uint64_t jiffies;

+ 6 - 2
include/kernel/mm.h

@@ -130,8 +130,11 @@ struct mempool {
 	struct mempool *next;
 };
 
-#define POOL_ENTRY_SIZE  64
-#define POOL_ENTRY_SHIFT 6ul
+#define DEVPOOL_ENTRY_SIZE 	1024
+#define DEVPOOL_ENTRY_SHIFT 	10ul
+
+#define POOL_ENTRY_SIZE  	64
+#define POOL_ENTRY_SHIFT	6ul
 
 extern struct mempool pool;
 extern struct mempool upool;
@@ -149,6 +152,7 @@ void *mm_vphy(void *);
 void trace_mm(void);
 
 #define invalidate() __asm__ __volatile__ ("movq %%cr3, %%rax\n\tmovq %%rax, %%cr3\n\t" ::: "ax")
+#define mfence() __asm__ __volatile__ ("mfence" ::: "memory")
 
 
 #endif

+ 3 - 1
include/kernel/panic.h

@@ -31,7 +31,9 @@
 #include <kernel/errno.h>
 
 
-void panic(char *);
+void panic(const char *);
+void syslog(const char *);
+void trace_syslog(void);
 
 
 #endif

+ 1 - 1
include/kernel/schk.h

@@ -42,8 +42,8 @@ void __stack_chk_fail(void);
 
 #define schk_init()					\
 	__uint64_t old_sp = get_rsp();			\
-	syslog("  entering function ");			\
 	syslog(__FUNCTION__);				\
+	syslog("\t");					\
 	if (old_sp < 0x3000) {				\
 		syslog("#1#PANIC: SP < 0x3000\n");	\
 		__stack_chk_fail();			\

+ 23 - 3
include/modules/ahci.h

@@ -56,7 +56,11 @@ struct ahci_hba_port {
 
 	__uint32_t cmd;
 
-	__uint32_t rsvd0[6];
+	__uint32_t rsvd0[3];
+
+	__uint32_t sata_status;
+	__uint32_t sata_ctl;
+	__uint32_t sata_error;
 
 	/* these look like bitmaps */
 	__uint32_t sata_active;
@@ -189,7 +193,23 @@ struct fis_h2d {
 #define ATACMD_WR 	0x30
 #define ATACMD_WR48 	0x34
 
-extern struct ahci_hba_mem *hba;
+#define ATASTAT_READY 	0x40
+#define ATASTAT_ERR 	0x01
+#define ATASTAT_BUSY 	0x80
+
+#define ATACTL_INTDIS 	0x02
+#define ATACTL_RESET 	0x04
+
+#define ATAERR_OTHER 	0x01
+#define ATAERR_TRACK0	0x02
+#define ATAERR_ABORT 	0x04
+#define ATAERR_NOTFOUND	0x10
+#define ATAERR_DATAERR 	0x40
+#define ATAERR_BADSECT 	0x80
 
-int ahci_rw_sect(struct ahci_hba_port *, int, char *, __uint64_t);
+
+extern volatile struct ahci_hba_mem *hba;
+
+int ahci_rw_sect(volatile struct ahci_hba_port *, int, char *, __uint64_t);
 void do_ahci_intr(void);
+

+ 15 - 4
init/main.c

@@ -77,17 +77,15 @@ time_t mktime(struct time *);
  * */
 
 int init(void) {
+	schk_init();
+
 	console_init();
 
 	print(red, "XNIX kernel starting...\n");
 
-	char buf[256] = {0};
-
 	int_init();
 	mm_init();
 
-	tty_read(&stdin, buf, 256);
-
 	task_init();
 	kbd_init();
 
@@ -95,6 +93,9 @@ int init(void) {
 
 	printk(white, "total %d devices. \n", pci_lspci());
 
+	trace_irq();
+	trace_syslog();
+
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
 	if (bootarg->bootdev & 0x80) 
@@ -105,10 +106,14 @@ int init(void) {
 
 	time_init();
 
+	schk_leave();
+
 	return 0;
 }
 
 void time_init(void) {
+	schk_init();
+
 	/* get current time, and save it to `startup_time` */
 	struct time tm;
 
@@ -139,6 +144,8 @@ void time_init(void) {
 
 	printk(white, "current unix time-stamp: %d. \n", mktime(&tm));
 
+	schk_leave();
+
 	return;
 }
 
@@ -160,6 +167,8 @@ static int month[] = {
 
 
 time_t mktime(struct time *tm) {
+	schk_init();
+
 	time_t res;
 	int year;
 
@@ -180,5 +189,7 @@ time_t mktime(struct time *tm) {
 	res += tm->min * MIN;
 	res += tm->sec;
 
+	schk_leave();
+
 	return res;
 }

+ 9 - 24
kernel/console.c

@@ -40,8 +40,6 @@
 
 static char buffer[256];
 
-
-static int syslog_index = 0;
 static __uint16_t *fb = FB_ADDR;
 __uint8_t x, y;
 
@@ -57,6 +55,8 @@ int nc_putc(struct tty *, char);
 
 
 errno_t console_init(void) {
+	schk_init();
+
 	x = bootarg->x, y = bootarg->y;
 
 	/* setup the frame buffer address fb */
@@ -87,6 +87,8 @@ errno_t console_init(void) {
 
 	/* well, this is the end */
 
+	schk_leave();
+
 	return 0;
 }
 
@@ -139,27 +141,7 @@ int nc_putc(struct tty *tty, char c) {
 }
 
 
-void syslog(const char *s) {
-	/* we can't use schk in this function, */
-	/* because this will make a die-loop (schk_init calls this function) */
-	int i;
-
-	for (i = 0; s[i]; i++) {
-		if (syslog_index == 32768) 
-			syslog_index = 0;
-	
-		*(syslog_index + SYSLOG_BUFFER) = s[i];			
-		syslog_index++;
-	}
-
-
-	return;
-}
-
-
 int printk(__uint8_t color, char *s, ...) {
-	schk_init();
-
 	va_list args;
 	int i;
 
@@ -177,8 +159,6 @@ int printk(__uint8_t color, char *s, ...) {
 
 	va_end(args);
 
-	schk_leave();
-
 	return i;
 }
 
@@ -231,6 +211,11 @@ void putc(char c, __uint8_t color) {
 		else 
 			x = ax;
 
+		if (x == VGA_X) {
+			y++;
+			x = TAB_SIZE;
+		}
+
 		update_cursor();
 		
 		return;

+ 6 - 1
kernel/int.c

@@ -131,7 +131,8 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 
 
 void die(const char *s, const __uint64_t *rsp, char nr) {
-	/* should we use schk here? */
+	schk_init();
+	
 	printk(red, "int #%b: %s. \n", nr, s);
 
 	printk(red, "rsp now: %q\n", rsp);
@@ -162,9 +163,13 @@ void die(const char *s, const __uint64_t *rsp, char nr) {
 	printk(red, "ds: %w\n", rsp[off_ds] & 0xffff);
 
 	trace_mm();
+	trace_irq();
+	trace_syslog();
 
 	for (;;) hlt();
 
+	schk_leave();
+
 	return;
 }
 

+ 25 - 1
kernel/irq.c

@@ -55,7 +55,7 @@ void register_irq(int nr, void *entry, const void *handler) {
 		mask = inb(0x21) & ~(1 << 2);
 		outb(mask, 0x21);
 
-		mask = inb(0xa1) & ~(1 << (nr-8));
+		mask = inb(0xa1) & ~(1 << (nr - 8));
 		outb(mask, 0xa1);
 	}
 
@@ -111,4 +111,28 @@ void do_irq(const __uint64_t *rsp, __uint64_t nr) {
 	return;
 }
 
+void trace_irq(void) {
+	schk_init();
+
+	printk(yellow, "tracing irq: \n");
+
+	__uint8_t slave = 0, master = 0;
+	int i;
+
+	master = inb(0x21);
+	slave = inb(0xa1);
+
+	printk(yellow, "\tmaster: %b  slave: %b\n");
+
+	for (i = 0; i < 8; i++) 
+		if (!(master & (1 << i))) 
+			printk(yellow, "\tirq %b (master) enabled. \n", i);
 
+	for (i = 0; i < 8; i++) 
+		if (!(slave & (1 << i))) 
+			printk(yellow, "\tirq %b (slave) enabled. \n", i + 8);
+
+	schk_leave();
+
+	return;
+}

+ 8 - 0
kernel/keyboard.c

@@ -64,11 +64,15 @@ errno_t kbd_init(void) {
 }
 
 void do_keyboard(void) {
+	schk_init();
+
 	__uint8_t code = inb(KBD_DATA);
 
 	/* insert it to the fifo */
 	fifo_put(&kbd_fifo, code);
 
+	schk_leave();
+
 	return;
 }
 
@@ -110,6 +114,8 @@ static char keycode_alphabet_shift[] = {
 };
 
 void put_keycode(void) {
+	schk_init();
+
 	__fifo_buf_t i;
 	i = fifo_get(&kbd_fifo);
 
@@ -223,5 +229,7 @@ undisplayable:
 
 	ext = 0;
 
+	schk_leave();
+
 	return;
 }

+ 51 - 3
kernel/mm.c

@@ -49,6 +49,8 @@ struct mempool pool, upool, devpool;
 
 
 void mm_init(void) {
+	schk_init();
+
 	mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
 	
 	struct e820_entry *e820 = E820_ADDR;
@@ -204,6 +206,8 @@ void mm_init(void) {
 
 	upool.next = &devpool;
 
+	schk_leave();
+
 	return;
 
 #undef z
@@ -212,6 +216,8 @@ void mm_init(void) {
 /* find an empty page table entry, and return its number */
 
 void *mapping(void *p, __uint64_t attr) {
+	schk_init();
+
 	__uint64_t i;
 	void *addr = (void *) ((__uint64_t) p & PAGE_MASK);
 
@@ -223,21 +229,30 @@ void *mapping(void *p, __uint64_t attr) {
 		if (!PT_ADDR[i]) {
 			PT_ADDR[i] = (__uint64_t) addr | attr;
 			invalidate();
+			schk_leave();
 			return (void *) ((i << PAGE_SHIFT) + p - addr);
 		}
 	}
 
+	schk_leave();
 	return NULL;
 #undef PT_ADDR
 }
 
 
 void *alloc_page(void) {
+	/* ...... */
+	schk_init();
+
+	schk_leave();
+
 	return alloc_page_att(PATT_KERNEL);
 }
 
 
 void *alloc_page_att(__uint64_t attr) {
+	schk_init();
+
 	__uint64_t i;
 
 	for (i = 0; bitmap_read(mm->bitmap, i); i++) {
@@ -256,25 +271,38 @@ void *alloc_page_att(__uint64_t attr) {
 		return NULL;
 	}
 
+	schk_leave();
+
 	return addr;
 }
-
+ 
 
 void free_page(void *p) {
+	schk_init();
+
 	__uint64_t i;
 
 	i = (__uint64_t) p >> PAGE_SHIFT;
 
 	bitmap_set(mm->bitmap, i, 0);
+	
+	schk_leave();
 
 	return;
 }
 
 
 void *kmalloc(struct mempool *pool, __uint64_t size) {
+	schk_init();
+
 	if (!size || size > pool->size - pool->used) 
 		return NULL;
 
+	/* devpool needs kb-aligning */
+
+	if (pool == &devpool) 
+		size = (size + 1023) & ~1023;
+
 	__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
 	__uint64_t i;
 
@@ -287,6 +315,7 @@ void *kmalloc(struct mempool *pool, __uint64_t size) {
 		bitmap_set(pool->bitmap, i, 1);
 		pool->used++;
 
+		schk_leave();
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 	}
 
@@ -316,6 +345,7 @@ repeat:
 
 		pool->used += points;
 
+		schk_leave();
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 	}
 
@@ -324,11 +354,18 @@ panic:
 	printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n", 
 		pool->size >> 10, pool->used << 6, points << 6);
 
+	schk_leave();
+
 	return NULL;
 }
 
 
 void kfree(struct mempool *pool, void *p, __uint64_t size) {
+	schk_init();
+
+	if (pool == &devpool) 
+		size = (size + 1023) & ~1023;
+	
 	__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
 	__uint64_t i;
 	
@@ -339,11 +376,15 @@ void kfree(struct mempool *pool, void *p, __uint64_t size) {
 
 	pool->used -= points;
 
+	schk_leave();
+
 	return;
 }
 
 
 void trace_mm(void) {
+	schk_init();
+
 	printk(yellow, "tracing system memory: \n");
 
 	__uint64_t *cr3 = (__uint64_t *) get_cr3();
@@ -356,10 +397,11 @@ void trace_mm(void) {
 
 	for (i = 0; i < 512; i++) {
 		if (pt[i] & ~0xff) 
-			printk(yellow, "%q -> %q: %b\n", i << PAGE_SHIFT, 
+			printk(yellow, "\t%q -> %q: %b\n", i << PAGE_SHIFT, 
 					pt[i] & ~0xff, pt[i] & 0xff);
 	}
 		
+	schk_leave();
 
 	return;
 }
@@ -372,6 +414,8 @@ void trace_mm(void) {
  * */
 
 void *mm_vphy(void *p) {
+	schk_init();
+
 	/* like mapping(), we just search in the first 1g */
 
 	__uint64_t addr = (__uint64_t) p & PAGE_MASK;
@@ -381,8 +425,12 @@ void *mm_vphy(void *p) {
 	__uint64_t i;
 
 	for (i = 0; i < 512; i++) 
-		if ((PT_ADDR[i] & ~0xff) == addr) 
+		if ((PT_ADDR[i] & ~0xff) == addr) {
+			schk_leave();
 			return (void *) ((i << PAGE_SHIFT) + ((__uint64_t) p - addr));
+		}
+
+	schk_leave();
 
 	return NULL;
 

+ 58 - 2
kernel/panic.c

@@ -29,7 +29,7 @@
 #include <kernel/types.h>
 
 #include <kernel/console.h>
-#include <kernel/int.h>
+#include <kernel/panic.h>
 
 #include <arch/x86/int.h>
 #include <arch/io.h>
@@ -37,10 +37,66 @@
 
 
 
+static int syslog_index = 0;
+
+
 void panic(const char *s) {
 	printk(red, "kernel panic: %s\n", s);
-	printk(red, "for more debug information, see syslog 0x90000\n");
+
+	trace_syslog();
 
 	for (;;) hlt();
 }
 
+void syslog(const char *s) {
+	/* we can't use schk in this function, */
+	/* because this will make a die-loop (schk_init calls this function) */
+	int i;
+
+	for (i = 0; s[i]; i++) {
+		if (syslog_index == 32768) 
+			syslog_index = 0;
+	
+		*(syslog_index + SYSLOG_BUFFER) = s[i];			
+		syslog_index++;
+	}
+
+
+	return;
+}
+
+
+void trace_syslog(void) {
+	printk(yellow, "tracing system log: \n");
+
+#if DEBUG_LEVEL != TEST_DEBUG_LEVEL
+	printk(yellow, "system log disabled. \n");
+	return;
+
+#else 
+	/* put last 8 calls to the screen */
+
+	int i;
+	char *fname = SYSLOG_BUFFER + syslog_index - 1;
+
+	for (i = 0; i < TRACE_SYSLOG_NR; i++) {
+		while (*--fname != '\t');
+
+		char *fn = fname + 1;
+
+		if (!(i % 3)) 
+			putc('\n', yellow);
+
+		print(yellow, "+ ");
+
+		while (*fn != '\t') 
+			putc(*fn++, yellow);
+
+		putc('\t', white);
+	}
+
+	putc('\n', yellow);
+
+	return;
+#endif
+}

+ 11 - 0
kernel/pci.c

@@ -31,6 +31,7 @@
 #include <kernel/int.h>
 #include <kernel/pci.h>
 #include <kernel/pci_vendors.h>
+#include <kernel/schk.h>
 
 #include <lib/string.h>
 
@@ -41,6 +42,8 @@
 
 
 __uint32_t pci_read_config(__uint8_t bus, __uint8_t slot, __uint8_t func, __uint8_t offset) {
+	schk_init();
+
 	__uint32_t addr;
 
 #define lbus  ((__uint32_t) bus)
@@ -55,6 +58,8 @@ __uint32_t pci_read_config(__uint8_t bus, __uint8_t slot, __uint8_t func, __uint
 }
 
 __uint32_t pci_lspci(void) {
+	schk_init();
+
 	__uint8_t bus, slot, func;
 	__uint32_t devs = 0;
 
@@ -75,11 +80,15 @@ __uint32_t pci_lspci(void) {
 		}
 	}
 
+	schk_leave();
+
 	return devs;
 }
 
 
 void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
+	schk_init();
+
 	__uint32_t buf[256];
 	struct pci_header header;
 	int i;
@@ -143,6 +152,8 @@ void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
 
 	dev_init(hdr);
 
+	schk_leave();
+
 	return;
 }
 

+ 47 - 5
kernel/sched.c

@@ -70,6 +70,8 @@ int do_exit(__uint64_t exitcode) {
 
 
 int task_init(void) {
+	schk_init();
+
 	/* initial the init-tss */
 
 	init_tss.rsp0 = init_tss.rsp1 = init_tss.rsp2 = get_rsp();
@@ -81,6 +83,8 @@ int task_init(void) {
 
 	current = &init_task;
 
+	schk_leave();
+
 	return 0;
 }
 
@@ -144,6 +148,8 @@ __asm__(
  * */
 
 int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
+	schk_init();
+
 	struct registers regs;
 
 	memset(&regs, 0x00, sizeof(regs));
@@ -154,10 +160,14 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
 	/* argument */
 	regs.rdi = arg;
 
+	schk_leave();
+
 	return do_fork(&regs, 0);
 }
 
 int utask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
+	schk_init();
+
 	struct registers regs;
 
 	memset(&regs, 0x00, sizeof(regs));
@@ -165,17 +175,25 @@ int utask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
 	regs.rip = (__uint64_t) entry;
 	regs.rdi = arg;
 
+	schk_leave();
+
 	return do_fork(&regs, 3);
 }
 
 
 
 static __uint32_t find_free_pid(void) {
+	schk_init();
+
 	__uint32_t freepid;
 
 	for (freepid = 0; freepid < MAX_TASKS; freepid++) 
-		if (!tasks[freepid]) 
+		if (!tasks[freepid]) {
+			schk_leave();
 			return freepid;
+		}
+
+	schk_leave();
 
 	return -1;
 }
@@ -195,6 +213,8 @@ static __uint32_t find_free_pid(void) {
  * */
 
 int do_fork(struct registers *regs, __uint64_t dpl) {
+	schk_init();
+
 	__uint64_t start_stack;
 
 	if (!dpl) 
@@ -203,8 +223,10 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 	else 
 		start_stack = (__uint64_t) kmalloc(&upool, 65536);
 
-	if (!start_stack) 
+	if (!start_stack) {
+		schk_leave();
 		return -ENOMEM;
+	}
 
 	/* += stack_size: point to the top of stack. */
 
@@ -212,8 +234,10 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 
 	struct task *child = kmalloc(&pool, sizeof(struct task));
 
-	if (!child) 
+	if (!child) {
+		schk_leave();
 		return -ENOMEM;
+	}
 
 	/* current is this task's father */
 	memcpy(child, current, sizeof(struct task));
@@ -233,8 +257,10 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 
 	child->pid = find_free_pid();
 
-	if (child->pid == -1) 
+	if (child->pid == -1) {
+		schk_leave();
 		return -EAGAIN;
+	}
 
 	printk(white, "new task pid: %d\n", child->pid);
 
@@ -303,11 +329,15 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 
 	tasks[child->pid] = child;
 
+	schk_leave();
+
 	return 0;
 }
 
 
 void set_tss(struct tss *table, __uint64_t dpl) {
+	schk_init();
+
 	__uint64_t tss = (__uint64_t) table;
 
 	/* create a descriptor (in gdt) and register it */
@@ -326,6 +356,8 @@ void set_tss(struct tss *table, __uint64_t dpl) {
 	/* load it to tr */
 	ltr(40);
 
+	schk_leave();
+
 	return;
 }
 
@@ -333,6 +365,8 @@ void set_tss(struct tss *table, __uint64_t dpl) {
 static char need_open_clock = 0;
 
 void schedule(void) {
+	schk_init();
+
 	/* find and switch to a new task */
 
 	__uint32_t pid;
@@ -368,17 +402,23 @@ void schedule(void) {
 		}
 	}
 	
-	if (current->pid == bestpid) 
+	if (current->pid == bestpid) {
+		schk_leave();
 		return;
+	}
 
 	/* will sti() in switch_to() */
 
 	switch_to(tasks[bestpid]);
 
+	schk_leave();
+
 	return;
 }
 
 void do_clock(void) {
+	schk_init();
+
 	current->counter--;
 
 	if (!current->counter) {
@@ -388,5 +428,7 @@ void do_clock(void) {
 		schedule();
 	}
 
+	schk_leave();
+
 	return;
 }

+ 10 - 0
kernel/sys.c

@@ -28,6 +28,8 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
+#include <kernel/schk.h>
+
 #include <sys/syscall.h>
 #include <kernel/console.h>
 
@@ -43,12 +45,18 @@ void *syscall_table[SYSCALL_NR] = {
 
 
 int no_syscall(void) {
+	schk_init();
+	
 	printk(yellow, "no such system call. ");
 
+	schk_leave();
+
 	return -ENOSYS;
 }
 
 int sys_print(char *s) {
+	schk_init();
+
 	printk(yellow, "sys_print: s = %q\n", s);
 
 	if (!s) 
@@ -56,5 +64,7 @@ int sys_print(char *s) {
 
 	print(yellow, s);
 
+	schk_leave();
+
 	return 0;
 }

+ 10 - 0
kernel/tty.c

@@ -27,7 +27,9 @@
 #include <kernel/init.h>
 #include <kernel/errno.h>
 #include <kernel/types.h>
+
 #include <kernel/console.h>
+#include <kernel/schk.h>
 
 #include <lib/fifo.h>
 
@@ -35,6 +37,8 @@
 /* read a character from tty (keyboard) with non-cooked */
 
 int tty_read(struct tty *tty, char *buf, size_t nbytes) {
+	schk_init();
+
 	if (!buf) 
 		return -EINVAL;
 
@@ -46,11 +50,15 @@ int tty_read(struct tty *tty, char *buf, size_t nbytes) {
 		/* read from stdout, or stderr */
 		return -EINVAL;
 
+	schk_leave();
+
 	return -EIO;
 }
 
 
 int tty_write(struct tty *tty, char *buf, size_t nbytes) {
+	schk_init();
+
 	if (!buf) 
 		return -EINVAL;
 
@@ -66,6 +74,8 @@ int tty_write(struct tty *tty, char *buf, size_t nbytes) {
 		tty->putc(tty, buf[i]);
 	}
 
+	schk_leave();
+
 	return i;
 }
 

+ 0 - 8
kernel/vsprintf.c

@@ -62,8 +62,6 @@
  * */
 
 int vsprintf(char *buf, const char *fmt, va_list args) {
-	schk_init();
-
 	/* ri: real index in buf */
 	__uint64_t i, ri;
 	char state, c;
@@ -130,16 +128,12 @@ int vsprintf(char *buf, const char *fmt, va_list args) {
 		}
 	}
 
-	schk_leave();
-
 	return ri;
 }
 
 
 
 int itos(char *buffer, unsigned long n, int base, int sign, int withzero, int width) {
-	schk_init();
-
 	static char digits[] = "0123456789abcdef";
 
 	char tbuf[16] = {0};
@@ -178,7 +172,5 @@ int itos(char *buffer, unsigned long n, int base, int sign, int withzero, int wi
 	while (--j >= 0) 
 		buffer[i - j - 1] = tbuf[j];
 
-	schk_leave();
-
 	return i;
 }

+ 119 - 47
modules/ahci.c

@@ -33,21 +33,26 @@
 
 #include <kernel/int.h>
 #include <kernel/panic.h>
+#include <kernel/schk.h>
 
 #include <arch/x86/paging.h>
 #include <modules/ahci.h>
 #include <lib/string.h>
 
 
-struct ahci_hba_mem *hba;
-static char buffer[512];
+volatile struct ahci_hba_mem *hba;
+static char buffer[512] = {0};
 
 
+build_irq(0x2a);
 build_irq(0x2e);
 build_irq(0x2f);
 
 
 errno_t ahci_init(struct pci_header0 *hdr) {
+	schk_init();
+
+	register_irq(10, irq0x2a_interrupt, do_ahci_intr);
 	register_irq(14, irq0x2e_interrupt, do_ahci_intr);
 	register_irq(15, irq0x2f_interrupt, do_ahci_intr);
 
@@ -65,89 +70,115 @@ errno_t ahci_init(struct pci_header0 *hdr) {
 	ahci_info("AHCI IRQ number: %b. \n", hdr->irqnr);
 	ahci_info("AHCI total ports: %d. \n", hba->cap & HBA_MEM_CAP_MASK);
 
-	hba->global_ctl |= HBA_MEM_GHC_AHCI_ENABLE;
-
-	ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
-
-	if (!(hba->bios_handoff & HBA_MEM_BHO_OWN_OS)) {
-		/* get the ownership */
-
-		hba->bios_handoff = (hba->bios_handoff & ~HBA_MEM_BHO_OWN_RWC) | 
-			HBA_MEM_BHO_OWN_OS;
-
-		int timeout = 4096;
+	if (hba->global_ctl & HBA_MEM_GHC_AHCI_ENABLE) 
+		ahci_info("AHCI is already in the AHCI mode. \n");
 
-		while (hba->bios_handoff & HBA_MEM_BHO_OWN_BIOS && timeout--);
+	else 
+		ahci_info("AHCI is in the IDE mode. \n");
 
-		if (!timeout) {
-			hba->bios_handoff = HBA_MEM_BHO_OWN_OS;
-			hba->bios_handoff |= HBA_MEM_BHO_OWN_CHANGED;
-		}
+	hba->global_ctl = 0x80000002;
 
-		ahci_info("got the ownership. \n");
-		ahci_info("current AHCI BIOS-handoff: %d\n", hba->bios_handoff);
-	}
+	mfence();
 
-	else 
-		ahci_info("AHCI is already in the OS mode. \n");
+	ahci_info("AHCI BIOS-handoff: %d\n", hba->bios_handoff);
 
 	int i, nrdevs = 0;
 
-	for (i = 0; i < (hba->cap & HBA_MEM_CAP_MASK); i++) {
+	for (i = 0; i <= (hba->cap & HBA_MEM_CAP_MASK); i++) {
 		/* if (bitmap_read(ports_impled, i) == 0) return; */
 		if (!(hba->ports_impled & (1 << i))) 
 			continue;
 
-		ahci_info("found ahci device %d on port %d. ", nrdevs++, i);
+		ahci_info("found ahci device %d on port %d. \n", nrdevs++, i);
 
-		struct ahci_cmd_header *cmdlist = kmalloc(&devpool, 
+		volatile struct ahci_cmd_header *cmdlist = kmalloc(&devpool, 
 				sizeof(struct ahci_cmd_header));
 
 		memset(cmdlist, 0x00, sizeof(struct ahci_cmd_header));
 
 		/* well, that needs a physical address, I think... */
-		hba->ports[i].cmd_list = (__uint64_t) mm_vphy(cmdlist);
+		hba->ports[i].cmd_list = (__uint64_t) mm_vphy((void *) cmdlist);
 
-		struct ahci_cmd_table *cmdtable = kmalloc(&devpool, 
+		volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool, 
 				sizeof(struct ahci_cmd_table));
 
 		memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
 
-		cmdlist->cmd_table = (__uint64_t) mm_vphy(cmdlist);
+		cmdlist->cmd_table = (__uint64_t) mm_vphy((void *) cmdlist);
 
 	}
 
-	ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 0);
+	hba->ports[0].cmd = hba->ports[0].cmd | 0x10000003;
+	hba->ports[0].sata_ctl = 0x04;
+
+	mfence();
+
+	ahci_info("SATA error: %x. \n", hba->ports[0].sata_error);
+	ahci_info("interrupt status: %x\n", hba->ports[0].int_status);
+
+	ahci_rw_sect(hba->ports + 0, ATACMD_RD, buffer, 2);
+
+	schk_leave();
 
 	return 0;
 }
 
 
-int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
+static void wait_controller_ready(volatile struct ahci_hba_port *port) {
+	schk_init();
+
+	while (port->sata_status & ATASTAT_BUSY && !(port->sata_status & ATASTAT_ERR));
+
+	ahci_info("wait_controller_ready: controller is now ready. \n");
+
+	schk_leave();
+
+	return;
+}
+
+
+int ahci_rw_sect(volatile struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_t lba) {
+	schk_init();
+
+	volatile char *devbuf = kmalloc(&devpool, 512);
+
+	wait_controller_ready(port);
+
+	if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48) 
+		memcpy(devbuf, buffer, 512);
+
 	ahci_info("ahci_rw: rw port %q %d with buffer %q, command %d...\n", 
 			port, lba, buffer, atacmd);
 
-	struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
+	port->sata_error = port->sata_error;
 
-	ahci_info("ahci_rw: command list: %q. \n", cmdhdr);
+	volatile struct ahci_cmd_header *cmdhdr = (struct ahci_cmd_header *) port->cmd_list;
+
+	ahci_info("ahci_rw: command list: %q. \n", mm_vphy((void *) cmdhdr));
 
 	memset(cmdhdr, 0x00, sizeof(struct ahci_cmd_header));
 
 	cmdhdr->config = 	(4 << CMDHDR_CFG_CFISLEN_SHIFT) | 
 				CMDHDR_CFG_CLEAR_R_OK | 
 				(0 << CMDHDR_CFG_PORTML_SHIFT) |
-				(1 << CMDHDR_CFG_PRDTLEN_SHIFT) |
-				((atacmd == ATACMD_WR || atacmd == ATACMD_WR48) ? 
-				CMDHDR_CFG_WRITE : 0);
+				(1 << CMDHDR_CFG_PRDTLEN_SHIFT);
+
+	if (atacmd == ATACMD_WR || atacmd == ATACMD_WR48) 
+		cmdhdr->config |= CMDHDR_CFG_WRITE;
 
 	cmdhdr->transfered = 0;
 
-	struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
+	volatile struct ahci_cmd_table *cmdtable = kmalloc(&devpool, sizeof(struct ahci_cmd_table));
+
+	ahci_info("ahci_rw: command table: %q. \n", mm_vphy((void *) cmdtable));
+
 	memset(cmdtable, 0x00, sizeof(struct ahci_cmd_table));
 
-	cmdhdr->cmd_table = (__uint64_t) mm_vphy(cmdtable);
+	cmdhdr->cmd_table = (__uint64_t) mm_vphy((void *) cmdtable);
+
+	volatile struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
 
-	struct fis_h2d *cmd_fis = kmalloc(&devpool, sizeof(struct fis_h2d));
+	ahci_info("ahci_rw: command fis: %q. \n", mm_vphy((void *) cmd_fis));
 
 	memset(cmd_fis, 0x00, sizeof(struct fis_h2d));
 
@@ -167,25 +198,66 @@ int ahci_rw_sect(struct ahci_hba_port *port, int atacmd, char *buffer, __uint64_
 	cmd_fis->dev = 0xe0;
 	cmd_fis->nsectors = 1;
 
-
 	memcpy(cmdtable->cmd_fis, cmd_fis, sizeof(struct fis_h2d));
-	cmdtable->prdts[0].buf = (__uint64_t) buffer;
+	cmdtable->prdts[0].buf = (__uint64_t) mm_vphy((void *) devbuf);
 	cmdtable->prdts[0].size = 511u | PRDT_SIZE_INTR;
 
-	port->int_enable = 1 << 5;
-	port->int_status = 1 << 5;
-	port->cmd_issue |= 1;
-	port->cmd |= 1;
+	port->int_enable = 0xffffffff;
+	port->int_status = 0xffffffff;
+	port->cmd_issue = 1;
+	port->sata_active = 1;
+
+	/* interrupt enable */
+	port->sata_ctl = 0x00;
+	
+	mfence();
 
 	/* wait it complete */
 
 	ahci_info("waiting device complete...\n");
 
-	while (!(port->int_status & (1 << 5)));
+	ahci_info("sata error: %q. \n", port->sata_error);
+	ahci_info("sata status: %q. \n", port->sata_status);
+
+	for (;;) {
+		/* task file error */
+		if (port->cmd_issue & (1 << 30)) {
+			ahci_info("ahci_rw_sect: controller error. \n");
+			goto panic;
+		}
+
+		if (!(port->cmd_issue & 1)) 
+			/* complete */
+			break;
+	}
+
+	if (port->sata_status & ATASTAT_ERR) {
+		ahci_info("ahci_rw_sect: controller error. \n");
+
+		ahci_info("sata error: %q\n", port->sata_status);
+
+		goto panic;
+	}
+
+	int i;
+
+	for (i = 0; i < 512 / 8; i++) 
+		printk(yellow, "%x", devbuf[i]);
+
 
-	ahci_info("AHCI request completed. \n");
+	ahci_info("\nAHCI request completed. \n");
 	port->cmd &= ~1;
 
+	if (atacmd == ATACMD_RD || atacmd == ATACMD_RD48) 
+		memcpy(buffer, devbuf, 512);
+
+panic:
+	kfree(&devpool, (void *) cmd_fis, sizeof(struct fis_h2d));
+	kfree(&devpool, (void *) cmdtable, sizeof(struct ahci_cmd_table));
+	kfree(&devpool, (void *) devbuf, 512);
+
+	schk_leave();
+
 	return 0;
 }