diff --git a/fs/super.c b/fs/super.c index b4be3389aa41510908dcf4c05e88159be9d788f9..34d638ea39c8df38509f105f27f932bbc6bc2876 100644 --- a/fs/super.c +++ b/fs/super.c @@ -147,10 +147,10 @@ void mount_root() { file_table[i].f_count=0; if (!(p = read_super(ROOT_DEV))) - printk("Unable to mount root"); + panic("Unable to mount root"); if (!(mi = iget(ROOT_DEV,ROOT_INO))) - printk("Unable to read root i-node"); + panic("Unable to read root i-node"); mi->i_count += 3 ; p->s_isup = p->s_imount = mi; diff --git a/include/linux/fs.h b/include/linux/fs.h index 6b77e93ca990ac8a16a3d06c5047e33065852f21..13d5db8417d86b0febad35795e51f3f288a5db83 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -148,6 +148,7 @@ extern void ll_rw_block(int rw, struct buffer_head * bh); extern void brelse(struct buffer_head * buf); extern struct buffer_head * bread(int dev,int block); +extern struct m_inode * namei(const char * pathname); extern int open_namei(const char * pathname, int flag, int mode, struct m_inode ** res_inode); diff --git a/include/linux/sched.h b/include/linux/sched.h index 64a7a3f5d0cbe3260620c96c586f71238cacb68c..0367ccfa821e9f92723fe3b2c8688a0586667a05 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -45,9 +45,6 @@ void wake_up(struct task_struct ** p); void test_a(); void test_b(); -extern struct task_struct *task[NR_TASKS]; -extern struct task_struct *current; - typedef int (*fn_ptr)(); struct tss_struct { @@ -117,6 +114,14 @@ struct task_struct { }, \ } +extern struct task_struct *task[NR_TASKS]; +extern struct task_struct *current; + +extern unsigned long volatile jiffies; +extern unsigned long startup_time; + +#define CURRENT_TIME (startup_time + jiffies / HZ) + /* * In linux is 4, because we add video selector, * so, it is 5 here. diff --git a/include/time.h b/include/time.h new file mode 100644 index 0000000000000000000000000000000000000000..7db589aea82e91e134fc7959d2a14c0a429dbccb --- /dev/null +++ b/include/time.h @@ -0,0 +1,42 @@ +#ifndef _TIME_H +#define _TIME_H + +#ifndef _TIME_T +#define _TIME_T +typedef long time_t; +#endif + +#ifndef _SIZE_T +#define _SIZE_T +typedef unsigned int size_t; +#endif + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#define CLOCKS_PER_SEC 100 + +typedef long clock_t; + +struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; +}; + +#define __isleap(year) \ + ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 1000 == 0)) + +clock_t clock(); +time_t time(time_t * tp); +time_t mktime(struct tm * tp); + +#endif + diff --git a/include/unistd.h b/include/unistd.h index 815c17b8570ebcd08e2121276a0dd2f78ed32852..6a810b4d356082ed90020a18ed98a9022d38d3cd 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -153,8 +153,12 @@ int dup(int fildes); int fork(); int fstat(); int setup(void *BIOS); +int sync(); int open(const char * filename, int flag, ...); int write(int fildes, const char * buf, off_t count); int read(int fildes, const char * buf, off_t count); +int rmdir(const char* pathname); +int chdir(const char* pathname); + #endif diff --git a/kernel/Makefile b/kernel/Makefile index d3c0c72f4df1deb04cc1e0463239bfe4ca95608a..8c976ab530a846818505373f4686fe1fcb71505d 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,7 +2,7 @@ GCC := gcc CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-pic -fno-stack-protector -c -m32 LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include -OBJS := head.o main.o sys_call.o asm.o sched.o printk.o vsprintf.o \ +OBJS := head.o main.o sys_call.o asm.o sched.o printk.o vsprintf.o mktime.o \ traps.o fork.o panic.o chr_drv/chr_drv.a blk_drv/blk_drv.a ../mm/mm.o ../lib/lib.a ../fs/fs.o system: $(OBJS) @@ -38,6 +38,9 @@ fork.o : fork.c panic.o : panic.c $(GCC) $(CCFLAG) -o $@ $< +mktime.o : mktime.c + $(GCC) $(CCFLAG) -o $@ $< + chr_drv/chr_drv.a: chr_drv/*.c cd chr_drv; make chr_drv.a; cd .. diff --git a/kernel/blk_drv/hd.c b/kernel/blk_drv/hd.c index 8a0c2bed7f229435221e74e29b4abd9714d2a3cf..0a41c49eef608e04eefac1d90234453a3597c026 100644 --- a/kernel/blk_drv/hd.c +++ b/kernel/blk_drv/hd.c @@ -135,21 +135,16 @@ int sys_setup(void * BIOS) { hd[i*5].nr_sects = 0; } - - for (i = 0; i < NR_HD; i++) { - printk("sectors for hd %d is %d\n", i, hd[i].nr_sects); - } - - //hd_identity(); - for (drive = 0; drive < NR_HD; drive++) { if (!(bh = bread(0x300 + drive*5,0))) { printk("Unable to read partition table of drive %d\n\r", drive); + panic(""); } if (bh->b_data[510] != 0x55 || (unsigned char) bh->b_data[511] != 0xAA) { printk("Bad partition table on drive %d\n\r",drive); + panic(""); } p = 0x1BE + (void *)bh->b_data; diff --git a/kernel/main.c b/kernel/main.c index 90b9e787dac2631f3697c7e570bfcaee814f5c84..908cda0565887dd32ec75a27bc3175ac3e8b0f39 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,15 +1,17 @@ #define __LIBRARY__ #include -#include +#include inline _syscall0(int, fork); -inline _syscall1(int,setup,void *,BIOS) +inline _syscall1(int, setup, void *, BIOS) +inline _syscall0(int, sync) int errno; #include +#include #include #include #include @@ -21,6 +23,8 @@ int errno; #include #include +#include + char printbuf[1024]; int printf(const char* fmt, ...); @@ -31,6 +35,7 @@ extern void hd_init(); extern void floppy_init(); extern void blk_dev_init(); extern void mem_init(long start, long end); +extern long kernel_mktime(struct tm * tm); #define EXT_MEM_K (*(unsigned short *)0x90002) #define DRIVE_INFO (*(struct drive_info *)0x90080) @@ -43,6 +48,34 @@ static long main_memory_start = 0; struct drive_info { char dummy[32]; } drive_info; +#define CMOS_READ(addr) ({ \ + outb_p(0x80|addr,0x70); \ + inb_p(0x71); \ +}) + +#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10) + +static void time_init() { + struct tm time; + do { + time.tm_sec = CMOS_READ(0); + time.tm_min = CMOS_READ(2); + time.tm_hour = CMOS_READ(4); + time.tm_mday = CMOS_READ(7); + time.tm_mon = CMOS_READ(8); + time.tm_year = CMOS_READ(9); + } while (time.tm_sec != CMOS_READ(0)); + + BCD_TO_BIN(time.tm_sec); + BCD_TO_BIN(time.tm_min); + BCD_TO_BIN(time.tm_hour); + BCD_TO_BIN(time.tm_mday); + BCD_TO_BIN(time.tm_mon); + BCD_TO_BIN(time.tm_year); + time.tm_mon--; + startup_time = kernel_mktime(&time); +} + void main(void) { ROOT_DEV = ORIG_ROOT_DEV; @@ -64,11 +97,10 @@ void main(void) mem_init(main_memory_start, memory_end); trap_init(); - sched_init(); tty_init(); - printk("\nmemory start at 0x%x, end at 0x%x\n", main_memory_start, memory_end); + time_init(); + sched_init(); buffer_init(buffer_memory_end); - blk_dev_init(); hd_init(); floppy_init(); @@ -128,6 +160,9 @@ void init() { else if (strcmp(a, "q") == 0) { break; } + else if (strcmp(a, "sy") == 0) { + sync(); + } } printf("Free mem: %d bytes\n\r",memory_end-main_memory_start); diff --git a/kernel/mktime.c b/kernel/mktime.c new file mode 100644 index 0000000000000000000000000000000000000000..7f8d664b77f04b5ae09d64e0bfc5d153fd7a99e4 --- /dev/null +++ b/kernel/mktime.c @@ -0,0 +1,39 @@ +#include + +#define MINUTE 60 +#define HOUR (60*MINUTE) +#define DAY (24*HOUR) +#define YEAR (365*DAY) + +static int month[12] = { + 0, + DAY*(31), + DAY*(31 + 29), + DAY*(31 + 29 + 31), + DAY*(31 + 29 + 31 + 30), + DAY*(31 + 29 + 31 + 30 + 31), + DAY*(31 + 29 + 31 + 30 + 31 + 30), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30), + DAY*(31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31) +}; + +long kernel_mktime(struct tm * tm) { + long res; + int year; + + year = tm->tm_year - 70; + res = YEAR*year + DAY*((year+1)/4); + res += month[tm->tm_mon]; + if (tm->tm_mon>1 && ((year+2)%4)) + res -= DAY; + res += DAY*(tm->tm_mday-1); + res += HOUR*tm->tm_hour; + res += MINUTE*tm->tm_min; + res += tm->tm_sec; + return res; +} + diff --git a/kernel/sched.c b/kernel/sched.c index 292a947636c02c3dde95b5b4dd51ad7408ccf6aa..01a9c4718f1dd6b849a29e3a4fc376584acd64b3 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -21,6 +21,9 @@ union task_union { static union task_union init_task = {INIT_TASK, }; //static union task_union second_task; +unsigned long volatile jiffies = 0; +unsigned long startup_time = 0; + struct task_struct * current = &(init_task.task); struct task_struct * task[NR_TASKS] = {&(init_task.task), }; diff --git a/kernel/sys_call.S b/kernel/sys_call.S index cd15ffda428e5e6ac019ccc715b070af82c67246..8cc3adb2d08e34da9a059108e07a1f9dd66588c0 100644 --- a/kernel/sys_call.S +++ b/kernel/sys_call.S @@ -64,6 +64,7 @@ timer_interrupt: movw %ax, %es movl $0x17, %eax movw %ax, %fs + incl jiffies movb $0x20, %al outb %al, $0x20 movl CS(%esp), %eax