diff --git a/include/console.h b/include/console.h index 783ca48..0880731 100644 --- a/include/console.h +++ b/include/console.h @@ -13,7 +13,7 @@ #ifndef _ORANGES_CONSOLE_H_ #define _ORANGES_CONSOLE_H_ - +#include "const.h" /* CONSOLE */ typedef struct s_console @@ -30,12 +30,18 @@ typedef struct s_console #define SCR_UP 1 /* scroll upward */ #define SCR_DN -1 /* scroll downward */ -#define SCR_SIZE (80 * 25) -#define SCR_WIDTH 80 - -#define DEFAULT_CHAR_COLOR (MAKE_COLOR(BLACK, WHITE)) -#define GRAY_CHAR (MAKE_COLOR(BLACK, BLACK) | BRIGHT) -#define RED_CHAR (MAKE_COLOR(BLUE, RED) | BRIGHT) +#define SCR_WIDTH 80 +#define SCR_HEIGHT 25 +#define SCR_SIZE (SCR_HEIGHT * SCR_WIDTH) +#define SCR_BUFSIZE (2 * SCR_SIZE) +#define SCR_MAXLINE (SCR_BUFSIZE / SCR_WIDTH) +#define FLASH_CHAR 0x8000 +#define DEFAULT_CHAR_COLOR ((MAKE_COLOR(BLACK, WHITE | BRIGHT)) << 8) +#define GRAY_CHAR (MAKE_COLOR(BLACK, BLACK) | BRIGHT) +#define RED_CHAR (MAKE_COLOR(BLUE, RED) | BRIGHT) +#define WHITE_CHAR (MAKE_COLOR(BLACK, WHITE | BRIGHT)) +#define MAKE_CELL(clr, ch) (clr | ch) +#define BLANK MAKE_CELL(DEFAULT_CHAR_COLOR, ' ') #endif /* _ORANGES_CONSOLE_H_ */ \ No newline at end of file diff --git a/include/const.h b/include/const.h index 284d050..6b28b0d 100644 --- a/include/const.h +++ b/include/const.h @@ -165,6 +165,7 @@ //added by mingxuan 2019-5-19 #define CRTC_ADDR_REG 0x3D4 /* CRT Controller Registers - Addr Register */ #define CRTC_DATA_REG 0x3D5 /* CRT Controller Registers - Data Register */ +#define UNDERLINE_REG 0x14 /* reg index of underline */ #define START_ADDR_H 0xC /* reg index of video mem start addr (MSB) */ #define START_ADDR_L 0xD /* reg index of video mem start addr (LSB) */ #define CURSOR_H 0xE /* reg index of cursor position (MSB) */ diff --git a/include/global.h b/include/global.h index 096d287..11d5ead 100644 --- a/include/global.h +++ b/include/global.h @@ -12,7 +12,6 @@ extern int kernel_initial; extern int ticks; -extern int disp_pos; extern u8 gdt_ptr[6]; // 0~15:Limit 16~47:Base extern DESCRIPTOR gdt[GDT_SIZE]; extern u8 idt_ptr[6]; // 0~15:Limit 16~47:Base diff --git a/include/proto.h b/include/proto.h index f4379c3..6d03253 100644 --- a/include/proto.h +++ b/include/proto.h @@ -6,10 +6,12 @@ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* klib.asm */ -void disp_str(char* info); -void disp_int(int input); -void disp_color_str(char* info, int color); -void write_char(char ch); //added by mingxuan 2019-5-19 + +void vga_set_disppos(int new_pos); +int vga_get_disppos(); +void vga_write_char(const char ch, u8 color); +void vga_write_str(const char * str); +void vga_write_str_color(const char * str, u8 color); //added by zcr void disable_irq(int irq); @@ -22,7 +24,6 @@ void init_prot(); u32 seg2phys(u16 seg); /* klib.c */ -void delay(int time); /* kernel.asm */ u32 read_cr2(); //add by visual 2016.5.9 diff --git a/include/tty.h b/include/tty.h index fae4111..7c75613 100644 --- a/include/tty.h +++ b/include/tty.h @@ -13,7 +13,7 @@ #ifndef _ORANGES_TTY_H_ #define _ORANGES_TTY_H_ - +#include "type.h" #define TTY_IN_BYTES 256 /* tty input queue size */ #define TTY_OUT_BUF_LEN 2 /* tty output buffer size */ diff --git a/include/uart.h b/include/uart.h new file mode 100644 index 0000000..4f8dd50 --- /dev/null +++ b/include/uart.h @@ -0,0 +1,12 @@ +#ifndef UART_H +#define UART_H + +#define PORT 0x3f8 // COM1 +#define SERIAL_BUF_SIZE 256 + + +int init_serial(); +char read_serial(); +void write_serial(char a); + +#endif \ No newline at end of file diff --git a/kernel/Makefrag b/kernel/Makefrag index 9efc253..5a36de5 100644 --- a/kernel/Makefrag +++ b/kernel/Makefrag @@ -30,8 +30,7 @@ KERN_SRCFILES :=kernel/kernel.asm \ kernel/i8259.c \ kernel/testfunc.c \ kernel/protect.c \ - lib/klib.c \ - lib/kliba.asm \ + kernel/uart.c \ KERN_OBJFILES := $(patsubst %.c, $(OBJDIR)/%.o, $(KERN_SRCFILES)) diff --git a/kernel/console.c b/kernel/console.c index 380c45d..d4138c5 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -48,7 +48,7 @@ void init_screen(TTY* tty) tty->console->current_line = 0; if(nr_tty==0){ - tty->console->cursor = disp_pos / 2; + tty->console->cursor = vga_get_disppos(); } const char prompt[] = "[TTY #?]\n"; @@ -87,15 +87,15 @@ void out_char(CONSOLE* con, char ch) con->cursor--; //*(pch - 2) = ' '; //*(pch - 1) = DEFAULT_CHAR_COLOR; - disp_pos = con->cursor*2; - write_char(' '); + vga_set_disppos(con->cursor); + vga_write_char(' ', WHITE_CHAR); } break; default: //*pch++ = ch; //*pch++ = DEFAULT_CHAR_COLOR; - disp_pos = con->cursor*2; - write_char(ch); + vga_set_disppos(con->cursor); + vga_write_char(ch, WHITE_CHAR); con->cursor++; break; @@ -144,7 +144,7 @@ static void clear_screen(int pos, int len) u8 * pch = (u8*)K_PHY2LIN(V_MEM_BASE + pos * 2); while (--len >= 0) { *pch++ = ' '; - *pch++ = DEFAULT_CHAR_COLOR; + *pch++ = (u8)DEFAULT_CHAR_COLOR; } } diff --git a/kernel/exec.c b/kernel/exec.c index 034f926..d47fea1 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -35,7 +35,7 @@ u32 sys_exec(char *path) if( 0==path ) { - disp_color_str("exec: path ERROR!",0x74); + vga_write_str_color("exec: path ERROR!",0x74); return -1; } @@ -86,7 +86,7 @@ u32 sys_exec(char *path) if( err_temp!=0 ) { - disp_color_str("kernel_main Error:lin_mapping_phy",0x74); + vga_write_str_color("kernel_main Error:lin_mapping_phy",0x74); return -1; } } @@ -98,9 +98,9 @@ u32 sys_exec(char *path) if (Echo_Phdr != NULL) sys_free(Echo_Phdr); - //disp_color_str("\n[exec success:",0x72);//灰底绿字 - //disp_color_str(path,0x72);//灰底绿字 - //disp_color_str("]",0x72);//灰底绿字 + //vga_write_str_color("\n[exec success:",0x72);//灰底绿字 + //vga_write_str_color(path,0x72);//灰底绿字 + //vga_write_str_color("]",0x72);//灰底绿字 return 0; } @@ -157,7 +157,7 @@ static u32 exec_load(u32 fd,const Elf32_Ehdr* Echo_Ehdr,const Elf32_Phdr Echo_Ph if( 0==Echo_Ehdr->e_phnum ) { - disp_color_str("exec_load: elf ERROR!",0x74); + vga_write_str_color("exec_load: elf ERROR!",0x74); return -1; } @@ -183,7 +183,7 @@ static u32 exec_load(u32 fd,const Elf32_Ehdr* Echo_Ehdr,const Elf32_Phdr Echo_Ph } else { - disp_color_str("exec_load: unKnown elf'program!",0x74); + vga_write_str_color("exec_load: unKnown elf'program!",0x74); return -1; } } diff --git a/kernel/fat32.c b/kernel/fat32.c index 19559b7..124e24a 100644 --- a/kernel/fat32.c +++ b/kernel/fat32.c @@ -179,7 +179,7 @@ STATE ReadFile(int fd,void *buf, int length) return ACCESSDENIED; } - disp_str("read:"); + kprintf("read:"); if(pfile->off>=pfile->size) { return 0; @@ -351,8 +351,8 @@ STATE OpenFile(const char *filename,int mode) GetNameFromPath(fullpath,name); state=PathToCluster(parent,&parentCluster); - disp_str("\nstate="); - disp_int(state); + kprintf("\nstate="); + kprintf("%d", state); if(state!=OK) { return -1; @@ -364,7 +364,7 @@ STATE OpenFile(const char *filename,int mode) { if(state == NAMEEXIST) //文件存在,使用O_CREAT是多余的,继续执行OpenFile即可 { - disp_str("file exists, O_CREAT is no use!"); + kprintf("file exists, O_CREAT is no use!"); } else //文件不存在,需要使用O_CREAT,先创建文件,再执行OpenFile { @@ -376,7 +376,7 @@ STATE OpenFile(const char *filename,int mode) { if(state != NAMEEXIST) //文件不存在,需要使用O_CREAT,用户没有使用,则报错并返回-1,表示路径有误 { - disp_str("no file, use O_CREAT!"); + kprintf("no file, use O_CREAT!"); return -1; } else{} //文件存在,使用O_CREAT是多余的,继续执行OpenFile即可 @@ -384,11 +384,11 @@ STATE OpenFile(const char *filename,int mode) //~mingxuan 2019-5-19 state=ReadRecord(parentCluster,name,&record,NULL,NULL); - disp_str("state="); - disp_int(state); + kprintf("state="); + kprintf("%d", state); if(state!=OK) { - disp_str("ReadRecord Fail!"); + kprintf("ReadRecord Fail!"); return -1; } @@ -403,9 +403,9 @@ STATE OpenFile(const char *filename,int mode) if ((fd < 0) || (fd >= NR_FILES)) { // panic("filp[] is full (PID:%d)", proc2pid(p_proc_current)); - disp_str("filp[] is full (PID:"); - disp_int(proc2pid(p_proc_current)); - disp_str(")\n"); + kprintf("filp[] is full (PID:"); + kprintf("%d", proc2pid(p_proc_current)); + kprintf(")\n"); return -1; } @@ -414,9 +414,9 @@ STATE OpenFile(const char *filename,int mode) if ((f_desc_table[i].flag == 0)) break; if (i >= NR_FILE_DESC) { - disp_str("f_desc_table[] is full (PID:"); - disp_int(proc2pid(p_proc_current)); - disp_str(")\n"); + kprintf("f_desc_table[] is full (PID:"); + kprintf("%d", proc2pid(p_proc_current)); + kprintf(")\n"); return -1; } @@ -428,9 +428,9 @@ STATE OpenFile(const char *filename,int mode) if (f_desc_table_fat[i].flag == 0) break; if (i >= NR_FILE_DESC) { - disp_str("f_desc_table[] is full (PID:"); - disp_int(proc2pid(p_proc_current)); - disp_str(")\n"); + kprintf("f_desc_table[] is full (PID:"); + kprintf("%d", proc2pid(p_proc_current)); + kprintf(")\n"); } //以下是给File结构体赋值 @@ -442,9 +442,9 @@ STATE OpenFile(const char *filename,int mode) f_desc_table_fat[i].off=0; f_desc_table_fat[i].size=record.filelength; f_desc_table_fat[i].flag=mode; - // disp_str("flag:"); + // kprintf("flag:"); // deint(f_desc_table_fat[i].flag); - // disp_str("index:"); + // kprintf("index:"); // deint(i); p_proc_current->task.filp[fd] ->fd_node.fd_file = &f_desc_table_fat[i]; @@ -552,7 +552,7 @@ STATE IsFile(PCHAR path,PUINT tag) void init_fs_fat() { - disp_str("Initializing fat32 file system... \n"); + kprintf("Initializing fat32 file system... \n"); buf = (u8*)K_PHY2LIN(sys_kmalloc(FSBUF_SIZE)); @@ -611,9 +611,9 @@ static void mkfs_fat() { driver_msg.PROC_NR = proc2pid(p_proc_current); hd_ioctl(&driver_msg); - disp_str("dev size: "); - disp_int(geo.size); - disp_str(" sectors\n"); + kprintf("dev size: "); + kprintf("%d", geo.size); + kprintf(" sectors\n"); TotalSectors = geo.size; @@ -870,7 +870,7 @@ int sys_ListDir(void *uesp) { // DirCheckup(array); // }else { // DisErrorInfo(state); - // disp_str("\n"); + // kprintf("\n"); // } // DestroyDArray(array); return 0; @@ -880,31 +880,31 @@ void DisErrorInfo(STATE state) { if(state==SYSERROR) { - disp_str(" system error\n"); + kprintf(" system error\n"); } else if(state==VDISKERROR) { - disp_str(" disk error\n"); + kprintf(" disk error\n"); } else if(state==INSUFFICIENTSPACE) { - disp_str(" no much space\n"); + kprintf(" no much space\n"); } else if(state==WRONGPATH) { - disp_str(" path error\n"); + kprintf(" path error\n"); } else if(state==NAMEEXIST) { - disp_str(" name exists\n"); + kprintf(" name exists\n"); } else if(state==ACCESSDENIED) { - disp_str(" deny access\n"); + kprintf(" deny access\n"); } else { - disp_str(" unknown error\n"); + kprintf(" unknown error\n"); } } diff --git a/kernel/fork.c b/kernel/fork.c index 7687ee6..c1eb777 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -28,7 +28,7 @@ int sys_fork() p_child = alloc_PCB(); if( 0==p_child ) { - disp_color_str("PCB NULL,fork faild!",0x74); + vga_write_str_color("PCB NULL,fork faild!",0x74); return -1; } else @@ -56,9 +56,9 @@ int sys_fork() /****************用户进程数+1****************************/ u_proc_sum += 1; - disp_color_str("[fork success:",0x72); - disp_color_str(p_proc_current->task.p_name,0x72); - disp_color_str("]",0x72); + vga_write_str_color("[fork success:",0x72); + vga_write_str_color(p_proc_current->task.p_name,0x72); + vga_write_str_color("]",0x72); //anything child need is prepared now, set its state to ready. added by xw, 17/12/11 p_child->task.stat = READY; diff --git a/kernel/global.c b/kernel/global.c index d5b43ae..b49ab0b 100644 --- a/kernel/global.c +++ b/kernel/global.c @@ -24,7 +24,6 @@ int kernel_initial; int ticks; -int disp_pos; u8 gdt_ptr[6]; DESCRIPTOR gdt[GDT_SIZE]; u8 idt_ptr[6]; diff --git a/kernel/kernel.asm b/kernel/kernel.asm index 5e079f7..e18a1c8 100644 --- a/kernel/kernel.asm +++ b/kernel/kernel.asm @@ -15,11 +15,9 @@ extern exception_handler extern spurious_irq extern clock_handler extern disp_str -extern delay extern irq_table extern page_fault_handler extern divide_error_handler ;added by xw, 18/12/22 -extern disp_int extern schedule extern switch_pde @@ -28,7 +26,6 @@ extern gdt_ptr extern idt_ptr extern p_proc_current extern tss -extern disp_pos extern k_reenter extern sys_call_table extern cr3_ready ;add by visual 2016.4.5 @@ -141,7 +138,6 @@ _start: ;mov esp, StackTop ; 堆栈在 bss 段中 mov esp, KernelStackTop ; 堆栈在 bss 段中 - mov dword [disp_pos], 0 sgdt [gdt_ptr] ; cstart() 中将会用到 gdt_ptr call cstart ; 在此函数中改变了gdt_ptr,让它指向新的GDT diff --git a/kernel/main.c b/kernel/main.c index 70f5d1a..b3c821b 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -32,13 +32,13 @@ int kernel_main() { int error; - disp_pos = 0; + vga_set_disppos(0); for (int i = 0; i < 25; i++) { for (int j = 0; j < 80; j++) { kprintf(" "); } } - disp_pos = 0; + vga_set_disppos(0); kprintf("-----Kernel Initialization Begins-----\n"); kernel_initial = 1; //kernel is in initial state. added by xw, 18/5/31 @@ -199,7 +199,7 @@ static int initialize_processes() /***************初始化PID进程页表*****************************/ if( 0 != init_page_pte(pid) ) { - disp_color_str("kernel_main Error:init_page_pte",0x74); + vga_write_str_color("kernel_main Error:init_page_pte",0x74); return -1; } // pde_addr_phy_temp = get_pde_phy_addr(pid);//获取该进程页目录物理地址 //delete by visual 2016.5.19 @@ -214,7 +214,7 @@ static int initialize_processes() //addr_phy_temp = (u32)do_kmalloc_4k();//为栈申请一个物理页,Task的栈是在内核里面 //delete by visual 2016.5.19 //if( addr_phy_temp<0 || (addr_phy_temp&0x3FF)!=0 ) //{ - // disp_color_str("kernel_main Error:addr_phy_temp",0x74); + // vga_write_str_color("kernel_main Error:addr_phy_temp",0x74); // return -1; //} err_temp = lin_mapping_phy( AddrLin,//线性地址 //add by visual 2016.5.9 @@ -224,7 +224,7 @@ static int initialize_processes() PG_P | PG_USU | PG_RWW);//页表的属性位 if( err_temp!=0 ) { - disp_color_str("kernel_main Error:lin_mapping_phy",0x74); + vga_write_str_color("kernel_main Error:lin_mapping_phy",0x74); return -1; } @@ -355,7 +355,7 @@ static int initialize_processes() /***************初始化PID进程页表*****************************/ if( 0 != init_page_pte(pid) ) { - disp_color_str("kernel_main Error:init_page_pte",0x74); + vga_write_str_color("kernel_main Error:init_page_pte",0x74); return -1; } //pde_addr_phy_temp = get_pde_phy_addr(pid);//获取该进程页目录物理地址 //edit by visual 2016.5.19 @@ -370,7 +370,7 @@ static int initialize_processes() //addr_phy_temp = (u32)do_kmalloc_4k();//为栈申请一个物理页,Task的栈是在内核里面 //delete by visual 2016.5.19 //if( addr_phy_temp<0 || (addr_phy_temp&0x3FF)!=0 ) //{ - // disp_color_str("kernel_main Error:addr_phy_temp",0x74); + // vga_write_str_color("kernel_main Error:addr_phy_temp",0x74); // return -1; //} err_temp = lin_mapping_phy( AddrLin,//线性地址 @@ -380,7 +380,7 @@ static int initialize_processes() PG_P | PG_USU | PG_RWW);//页表的属性位 if( err_temp!=0 ) { - disp_color_str("kernel_main Error:lin_mapping_phy",0x74); + vga_write_str_color("kernel_main Error:lin_mapping_phy",0x74); return -1; } diff --git a/kernel/memman.c b/kernel/memman.c index b84c167..ec5c507 100644 --- a/kernel/memman.c +++ b/kernel/memman.c @@ -340,22 +340,22 @@ void memman_test() p1 = (u32 *)do_malloc(4); if(-1 != (u32)p1){ //打印p1,当前空闲内存信息,p1所指内存的内容 - disp_str("START"); - disp_int((u32)p1); + kprintf("START"); + kprintf("%d", (u32)p1); //disp_free(); *p1 = TEST; - disp_int(*p1); - disp_str("END"); + kprintf("%d", *p1); + kprintf("END"); } p2 = (u32 *)do_kmalloc(4); if(-1 != (u32)p2){ - disp_str("START"); - disp_int((u32)p2); + kprintf("START"); + kprintf("%d", (u32)p2); //disp_free(); *p2 = TEST; - disp_int(*p2); - disp_str("END"); + kprintf("%d", *p2); + kprintf("END"); } do_free((u32)p1,4); @@ -363,42 +363,42 @@ void memman_test() p3 = (u32 *)do_malloc_4k(); if(-1 != (u32)p3){ - disp_str("START"); - disp_int((u32)p3); + kprintf("START"); + kprintf("%d", (u32)p3); //disp_free(); *p3 = TEST; - disp_int(*p3); + kprintf("%d", *p3); p = p3 + 2044; *p = 0x22334455; - disp_int(*p); + kprintf("%d", *p); p += 2048; *p = 0x33445566; - disp_int(*p); - disp_str("END"); + kprintf("%d", *p); + kprintf("END"); } p4 = (u32 *)do_kmalloc_4k(4); if(-1 != (u32)p4){ - disp_str("START"); - disp_int((u32)p4); + kprintf("START"); + kprintf("%d", (u32)p4); //disp_free(); *p4 = TEST; - disp_int(*p4); + kprintf("%d", *p4); p = p4 + 2044; *p = 0x22334455; - disp_int(*p); + kprintf("%d", *p); p += 2048; *p = 0x33445566; - disp_int(*p); - disp_str("END"); + kprintf("%d", *p); + kprintf("END"); } do_free_4k((u32)p3); do_free_4k((u32)p4); - disp_str("START"); + kprintf("START"); disp_free(); - disp_str("END"); + kprintf("END"); return; } diff --git a/kernel/pagetbl.c b/kernel/pagetbl.c index 2b9d840..e2ccc5e 100644 --- a/kernel/pagetbl.c +++ b/kernel/pagetbl.c @@ -10,6 +10,7 @@ #include "global.h" #include "proto.h" #include "memman.h" +#include "stdio.h" // to determine if a page fault is reparable. added by xw, 18/6/11 u32 cr2_save; @@ -38,7 +39,7 @@ u32 init_page_pte(u32 pid) if (pde_addr_phy_temp < 0 || (pde_addr_phy_temp & 0x3FF) != 0) // add by visual 2016.5.9 { - disp_color_str("init_page_pte Error:pde_addr_phy_temp", 0x74); + vga_write_str_color("init_page_pte Error:pde_addr_phy_temp", 0x74); return -1; } @@ -55,7 +56,7 @@ u32 init_page_pte(u32 pid) PG_P | PG_USS | PG_RWW); //页表的属性位(系统权限) //edit by visual 2016.5.17 if (err_temp != 0) { - disp_color_str("init_page_pte Error:lin_mapping_phy", 0x74); + vga_write_str_color("init_page_pte Error:lin_mapping_phy", 0x74); return -1; } } @@ -81,18 +82,18 @@ void page_fault_handler(u32 vec_no, //异常编号,此时应该是14,代 // if page fault happens in kernel, it's an error. if (kernel_initial == 1) { - disp_str("\n"); - disp_color_str("Page Fault\n", 0x74); - disp_color_str("eip=", 0x74); //灰底红字 - disp_int(eip); - disp_color_str("eflags=", 0x74); - disp_int(eflags); - disp_color_str("cs=", 0x74); - disp_int(cs); - disp_color_str("err_code=", 0x74); - disp_int(err_code); - disp_color_str("Cr2=", 0x74); //灰底红字 - disp_int(cr2); + kprintf("\n"); + vga_write_str_color("Page Fault\n", 0x74); + vga_write_str_color("eip=", 0x74); //灰底红字 + kprintf("%d", eip); + vga_write_str_color("eflags=", 0x74); + kprintf("%d", eflags); + vga_write_str_color("cs=", 0x74); + kprintf("%d", cs); + vga_write_str_color("err_code=", 0x74); + kprintf("%d", err_code); + vga_write_str_color("Cr2=", 0x74); //灰底红字 + kprintf("%d", cr2); halt(); } @@ -106,26 +107,26 @@ void page_fault_handler(u32 vec_no, //异常编号,此时应该是14,代 cr2_count++; if (cr2_count == 5) { - disp_str("\n"); - disp_color_str("Page Fault\n", 0x74); - disp_color_str("eip=", 0x74); //灰底红字 - disp_int(eip); - disp_color_str("eflags=", 0x74); - disp_int(eflags); - disp_color_str("cs=", 0x74); - disp_int(cs); - disp_color_str("err_code=", 0x74); - disp_int(err_code); - disp_color_str("Cr2=", 0x74); //灰底红字 - disp_int(cr2); - disp_color_str("Cr3=", 0x74); - disp_int(p_proc_current->task.cr3); + kprintf("\n"); + vga_write_str_color("Page Fault\n", 0x74); + vga_write_str_color("eip=", 0x74); //灰底红字 + kprintf("%d", eip); + vga_write_str_color("eflags=", 0x74); + kprintf("%d", eflags); + vga_write_str_color("cs=", 0x74); + kprintf("%d", cs); + vga_write_str_color("err_code=", 0x74); + kprintf("%d", err_code); + vga_write_str_color("Cr2=", 0x74); //灰底红字 + kprintf("%d", cr2); + vga_write_str_color("Cr3=", 0x74); + kprintf("%d", p_proc_current->task.cr3); //获取页目录中填写的内容 - disp_color_str("Pde=", 0x74); - disp_int(*((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2))); + vga_write_str_color("Pde=", 0x74); + kprintf("%d", *((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2))); //获取页表中填写的内容 - disp_color_str("Pte=", 0x74); - disp_int(*((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))); + vga_write_str_color("Pte=", 0x74); + kprintf("%d", *((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))); halt(); } } @@ -137,15 +138,15 @@ void page_fault_handler(u32 vec_no, //异常编号,此时应该是14,代 if (0 == pte_exist(pde_addr_phy_temp, cr2)) { //页表不存在 - // disp_color_str("[Pde Fault!]",0x74); //灰底红字 + // vga_write_str_color("[Pde Fault!]",0x74); //灰底红字 (*((u32 *)K_PHY2LIN(pde_addr_phy_temp) + get_pde_index(cr2))) |= PG_P; - // disp_color_str("[Solved]",0x74); + // vga_write_str_color("[Solved]",0x74); } else { //只是缺少物理页 - // disp_color_str("[Pte Fault!]",0x74); //灰底红字 + // vga_write_str_color("[Pte Fault!]",0x74); //灰底红字 (*((u32 *)K_PHY2LIN(pte_addr_phy_temp) + get_pte_index(cr2))) |= PG_P; - // disp_color_str("[Solved]",0x74); + // vga_write_str_color("[Solved]",0x74); } refresh_page_cache(); } @@ -309,7 +310,7 @@ int lin_mapping_phy(u32 AddrLin, //线性地址 if (pte_addr_phy < 0 || (pte_addr_phy & 0x3FF) != 0) // add by visual 2016.5.9 { - disp_color_str("lin_mapping_phy Error:pte_addr_phy", 0x74); + vga_write_str_color("lin_mapping_phy Error:pte_addr_phy", 0x74); return -1; } @@ -332,7 +333,7 @@ int lin_mapping_phy(u32 AddrLin, //线性地址 phy_addr = do_kmalloc_4k(); //从内核物理地址申请一页 else { - // disp_str("%"); + // kprintf("%"); phy_addr = do_malloc_4k(); //从用户物理地址空间申请一页 } } @@ -349,7 +350,7 @@ int lin_mapping_phy(u32 AddrLin, //线性地址 if (phy_addr < 0 || (phy_addr & 0x3FF) != 0) { - disp_color_str("lin_mapping_phy:phy_addr ERROR", 0x74); + vga_write_str_color("lin_mapping_phy:phy_addr ERROR", 0x74); return -1; } diff --git a/kernel/protect.c b/kernel/protect.c index 85cab81..28f3d20 100644 --- a/kernel/protect.c +++ b/kernel/protect.c @@ -12,6 +12,7 @@ #include "global.h" #include "proto.h" #include "string.h" +#include "stdio.h" /* 本文件内函数声明 */ @@ -270,29 +271,30 @@ static void init_descriptor(DESCRIPTOR * p_desc, u32 base, u32 limit, u16 attrib }; /* 通过打印空格的方式清空屏幕的前五行,并把 disp_pos 清零 */ - disp_pos = 0; + vga_set_disppos(0); for(i=0;i<80*5;i++){ - disp_str(" "); + kprintf(" "); } - disp_pos = 0; + vga_set_disppos(0); - disp_color_str("Exception! --> ", text_color); - disp_color_str(err_description[vec_no], text_color); - disp_color_str("\n\n", text_color); - disp_color_str("EFLAGS:", text_color); - disp_int(eflags); - disp_color_str("CS:", text_color); - disp_int(cs); - disp_color_str("EIP:", text_color); - disp_int(eip); + + vga_write_str_color("Exception! --> ", text_color); + vga_write_str_color(err_description[vec_no], text_color); + vga_write_str_color("\n\n", text_color); + vga_write_str_color("EFLAGS:", text_color); + kprintf("%d", eflags); + vga_write_str_color("CS:", text_color); + kprintf("%d", cs); + vga_write_str_color("EIP:", text_color); + kprintf("%d", eip); if(err_code != 0xFFFFFFFF){ - disp_color_str("Error code:", text_color); - disp_int(err_code); + vga_write_str_color("Error code:", text_color); + kprintf("%d", err_code); } //added by xw, 18/12/19 - disp_str("\n"); + kprintf("\n"); //added by xw, 18/12/19 p_proc_current->task.stat = KILLED; @@ -320,7 +322,7 @@ static void init_descriptor(DESCRIPTOR * p_desc, u32 base, u32 limit, u16 attrib while (1) { - disp_str("Loop in divide error handler...\n"); + kprintf("Loop in divide error handler...\n"); i = 100; while(--i){ diff --git a/kernel/pthread.c b/kernel/pthread.c index 04a9cc7..b942573 100644 --- a/kernel/pthread.c +++ b/kernel/pthread.c @@ -27,16 +27,16 @@ int sys_pthread(void *entry) /*if(p_proc_current->task.info.type == TYPE_THREAD ) {//线程不能创建线程 - disp_color_str("[pthread failed:",0x74); - disp_color_str(p_proc_current->task.p_name,0x74); - disp_color_str("]",0x74); + vga_write_str_color("[pthread failed:",0x74); + vga_write_str_color(p_proc_current->task.p_name,0x74); + vga_write_str_color("]",0x74); return -1; }*/ /*****************申请空白PCB表**********************/ p_child = alloc_PCB(); if( 0==p_child ) { - disp_color_str("PCB NULL,pthread faild!",0x74); + vga_write_str_color("PCB NULL,pthread faild!",0x74); return -1; } else @@ -78,9 +78,9 @@ int sys_pthread(void *entry) /****************用户进程数+1****************************/ u_proc_sum += 1; - disp_color_str("[pthread success:",0x72); - disp_color_str(p_proc_current->task.p_name,0x72); - disp_color_str("]",0x72); + vga_write_str_color("[pthread success:",0x72); + vga_write_str_color(p_proc_current->task.p_name,0x72); + vga_write_str_color("]",0x72); //anything child need is prepared now, set its state to ready. added by xw, 17/12/11 p_child->task.stat = READY; diff --git a/kernel/start.c b/kernel/start.c index 9d8fe19..9c44511 100644 --- a/kernel/start.c +++ b/kernel/start.c @@ -57,6 +57,7 @@ _warn(const char *file, int line, const char *fmt,...) *======================================================================*/ void cstart() { + vga_set_disppos(0); kprintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----\"cstart\" begins-----\n"); // 将 LOADER 中的 GDT 复制到新的 GDT 中 diff --git a/kernel/syscallc.c b/kernel/syscallc.c index cb4d2b3..2a2170e 100644 --- a/kernel/syscallc.c +++ b/kernel/syscallc.c @@ -13,6 +13,7 @@ #include "global.h" #include "proto.h" #include "memman.h" +#include "stdio.h" struct memfree *memarg = 0; @@ -121,7 +122,7 @@ int sys_free_4k(void* AddrLin) *======================================================================*/ void sys_udisp_int(int arg) { - disp_int(arg); + kprintf("%d", arg); return ; } @@ -131,6 +132,6 @@ void sys_udisp_int(int arg) *======================================================================*/ void sys_udisp_str(char *arg) { - disp_str(arg); + kprintf(arg); return ; } \ No newline at end of file diff --git a/kernel/testfunc.c b/kernel/testfunc.c index bfe3c84..bbde009 100644 --- a/kernel/testfunc.c +++ b/kernel/testfunc.c @@ -11,6 +11,7 @@ #include "proc.h" #include "global.h" #include "proto.h" +#include "stdio.h" /* * This syscall needs long time to finish, so we can use it @@ -21,7 +22,7 @@ void sys_print_E() { int i, j; - disp_str("E( "); + kprintf("E( "); i = 100; while(--i){ @@ -29,7 +30,7 @@ void sys_print_E() while(--j){} } - disp_str(") "); + kprintf(") "); } /* @@ -41,7 +42,7 @@ void sys_print_F() { int i, j; - disp_str("F( "); + kprintf("F( "); i = 100; while(--i){ @@ -49,5 +50,5 @@ void sys_print_F() while(--j){} } - disp_str(") "); + kprintf(") "); } diff --git a/kernel/tty.c b/kernel/tty.c index 3f21ada..84177bf 100644 --- a/kernel/tty.c +++ b/kernel/tty.c @@ -99,9 +99,9 @@ void task_tty() //设置第一个tty光标位置,第一个tty需要特殊处理 disable_int(); outb(CRTC_ADDR_REG, CURSOR_H); - outb(CRTC_DATA_REG, ((disp_pos / 2) >> 8) & 0xFF); + outb(CRTC_DATA_REG, ((vga_get_disppos()) >> 8) & 0xFF); outb(CRTC_ADDR_REG, CURSOR_L); - outb(CRTC_DATA_REG, (disp_pos / 2) & 0xFF); + outb(CRTC_DATA_REG, (vga_get_disppos()) & 0xFF); enable_int(); //轮询 diff --git a/kernel/uart.c b/kernel/uart.c new file mode 100644 index 0000000..b438d40 --- /dev/null +++ b/kernel/uart.c @@ -0,0 +1,54 @@ +#include "uart.h" +#include "x86.h" +#include "tty.h" +#include "const.h" +#include "type.h" +#include "assert.h" +#include "stdio.h" +#include "memman.h" + + +void put_irq_handler(int irq, irq_handler handler); +void enable_irq(int irq); + +int init_serial() { + outb(PORT + 1, 0x00); // Disable all interrupts + outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) + outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud + outb(PORT + 1, 0x00); // (hi byte) + outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit + outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set + outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip + outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) + + // Check if serial is faulty (i.e: not same byte as sent) + if(inb(PORT + 0) != 0xAE) { + assert(0); + return 1; + } + + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + outb(PORT + 4, 0x0f); + return 0; +} +static inline int is_transmit_empty() { + return inb(PORT + 5) & 0x20; +} + +static inline int serial_received() { + return inb(PORT + 5) & 1; +} + +char read_serial() { + while (serial_received() == 0); + + return inb(PORT); +} + +void write_serial(char a) { + while (is_transmit_empty() == 0); + + outb(PORT,a); +} \ No newline at end of file diff --git a/lib/Makefrag b/lib/Makefrag index 27b511d..e43dc83 100644 --- a/lib/Makefrag +++ b/lib/Makefrag @@ -6,6 +6,7 @@ LIB_OBJS := lib/string.o \ lib/kprintf.o \ lib/scanf.o \ lib/printfmt.o \ + lib/vga_display.o \ LIB_OBJS := $(patsubst %, $(OBJDIR)/%, $(LIB_OBJS)) diff --git a/lib/klib.c b/lib/klib.c deleted file mode 100644 index 7c443e3..0000000 --- a/lib/klib.c +++ /dev/null @@ -1,75 +0,0 @@ - -/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - klib.c -++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - Forrest Yu, 2005 -++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ - -#include "type.h" -#include "const.h" -#include "protect.h" -#include "string.h" -#include "proc.h" -#include "global.h" -#include "proto.h" - - -/*======================================================================* - itoa - *======================================================================*/ - char * itoa(char * str, int num)/* 数字前面的 0 不被显示出来, 比如 0000B800 被显示成 B800 */ -{ - char * p = str; - char ch; - int i; - int flag = FALSE; - - *p++ = '0'; - *p++ = 'x'; - - if(num == 0){ - *p++ = '0'; - } - else{ - for(i=28;i>=0;i-=4){ - ch = (num >> i) & 0xF; - if(flag || (ch > 0)){ - flag = TRUE; - ch += '0'; - if(ch > '9'){ - ch += 7; - } - *p++ = ch; - } - } - } - - *p = 0; - - return str; -} - - -/*======================================================================* - disp_int - *======================================================================*/ - void disp_int(int input) -{ - char output[16]; - itoa(output, input); - disp_str(output); -} - -/*======================================================================* - delay - *======================================================================*/ - void delay(int time) -{ - int i, j, k; - for(k=0;k> 8) & 0xFF); + outb(CRTC_ADDR_REG, CURSOR_L); + outb(CRTC_DATA_REG, position & 0xFF); + enable_int(); +} + +/***************************************************************************** + Disable the blinking cursor +*/ +static inline void vga_disable_cursor() +{ + outb(CRTC_ADDR_REG, 0x0A); + outb(CRTC_DATA_REG, 0x20); +} + +/***************************************************************************** + Enable the blinking cursor, and set the height of the cursor + The cursor is splited into 16 units + @param cursor_start the start unit index + @param cursor_end the ending unit index +*/ +static inline void vga_enable_cursor(u8 cursor_start, u8 cursor_end) +{ + outb(0x3D4, 0x0A); + outb(0x3D5, (inb(0x3D5) & 0xC0) | cursor_start); + + outb(0x3D4, 0x0B); + outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end); +} + +/***************************************************************************** + Write data directly to Video Memory cell + @param pos text mode position(pos*2 yourself) + @param dat data to be written, with format [ BG | FG | ASCII ] +*/ +static inline void vga_put_raw(u32 pos, u16 dat) +{ + u16 *pch = (u16 *)K_PHY2LIN(V_MEM_BASE + pos); + *pch = dat; +} + +static inline void vga_flush_blankline(int line_no) +{ + u32 *dst = (u32 *)K_PHY2LIN(V_MEM_BASE + line_no * SCR_WIDTH * 2); + for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i) + { + *dst++ = (BLANK << 16) | BLANK; + } +} + +static int disp_pos = 0; + +void vga_set_disppos(int new_pos) { + disp_pos = new_pos; +} +int vga_get_disppos() { + return disp_pos; +} + +/********************************************************** + * Write char to disp_pos and then increase disp_pos + * @param ch the character + * @param color the color, in 8-bit format +*/ +void vga_write_char(const char ch, u16 color) { + if (ch == '\n') { + disp_pos = (disp_pos / SCR_WIDTH + 1) * SCR_WIDTH; + } + else { + vga_put_raw(disp_pos * 2, (color << 8) | (u16)ch); + disp_pos ++; + } + vga_set_cursor(disp_pos); +} + +void vga_write_str(const char * str) { + for (char* s = (char *)str; *s; ++s) { + vga_write_char(*s, WHITE_CHAR); + } +} + +void vga_write_str_color(const char * str, u8 color) { + for (char* s = (char *)str; *s; ++s) { + vga_write_char(*s, color); + } +} \ No newline at end of file