diff --git a/include/console.h b/include/console.h index 83c4989..660e185 100644 --- a/include/console.h +++ b/include/console.h @@ -1,45 +1,43 @@ -/*************************************************************************//** - ***************************************************************************** - * @file console.h - * @brief - * @author Forrest Y. Yu - * @date 2005 - ***************************************************************************** - *****************************************************************************/ +/*************************************************************************/ /** + ***************************************************************************** + * @file console.h + * @brief + * @author Forrest Y. Yu + * @date 2005 + ***************************************************************************** + *****************************************************************************/ /********************************************************** -* console.h //added by mingxuan 2019-5-17 -***********************************************************/ - -#ifndef _ORANGES_CONSOLE_H_ -#define _ORANGES_CONSOLE_H_ + * console.h //added by mingxuan 2019-5-17 + ***********************************************************/ +#ifndef _ORANGES_CONSOLE_H_ +#define _ORANGES_CONSOLE_H_ /* CONSOLE */ typedef struct s_console { - unsigned int crtc_start; /* set CRTC start addr reg */ - unsigned int orig; /* start addr of the console */ - unsigned int con_size; /* how many words does the console have */ - unsigned int cursor; - int is_full; - unsigned int current_line; -}CONSOLE; + unsigned int crtc_start; /* set CRTC start addr reg */ + unsigned int orig; /* start addr of the console */ + unsigned int con_size; /* how many words does the console have */ + unsigned int cursor; + int is_full; + unsigned int current_line; +} CONSOLE; +#define SCR_UP 1 /* scroll upward */ +#define SCR_DN -1 /* scroll downward */ -#define SCR_UP 1 /* scroll upward */ -#define SCR_DN -1 /* scroll downward */ +#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 SCR_WIDTH 80 -#define SCR_HEIGHT 25 -#define SCR_SIZE (SCR_HEIGHT * SCR_WIDTH) -#define SCR_BUFSIZE (8 * SCR_SIZE) -#define SCR_MAXLINE (SCR_BUFSIZE / SCR_WIDTH) - -#define DEFAULT_CHAR_COLOR (MAKE_COLOR(BLACK, WHITE | BRIGHT)) -#define GRAY_CHAR (MAKE_COLOR(BLACK, BLACK) | BRIGHT) -#define RED_CHAR (MAKE_COLOR(BLUE, RED) | BRIGHT) -#define MAKE_CELL(clr, ch) ((clr << 8) | ch) -#define BLANK MAKE_CELL(DEFAULT_CHAR_COLOR, ' ') +#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 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/tty.h b/include/tty.h index 9d69934..f15b85d 100644 --- a/include/tty.h +++ b/include/tty.h @@ -1,31 +1,30 @@ -/*************************************************************************//** - ***************************************************************************** - * @file tty.h - * @brief - * @author Forrest Y. Yu - * @date 2005 - ***************************************************************************** - *****************************************************************************/ +/*************************************************************************/ /** + ***************************************************************************** + * @file tty.h + * @brief + * @author Forrest Y. Yu + * @date 2005 + ***************************************************************************** + *****************************************************************************/ /********************************************************** -* tty.h //added by mingxuan 2019-5-17 -***********************************************************/ + * tty.h //added by mingxuan 2019-5-17 + ***********************************************************/ -#ifndef _ORANGES_TTY_H_ -#define _ORANGES_TTY_H_ +#ifndef _ORANGES_TTY_H_ +#define _ORANGES_TTY_H_ - -#define TTY_IN_BYTES 256 /* tty input queue size */ -#define TTY_OUT_BUF_LEN 2 /* tty output buffer size */ +#define TTY_IN_BYTES 256 /* tty input queue size */ +#define TTY_OUT_BUF_LEN 2 /* tty output buffer size */ /* TTY state (3bit) wait_enter wait_space display - 1/0 1/0 1/0 + 1/0 1/0 1/0 */ -#define TTY_STATE_WAIT_ENTER 4 /*100*/ -#define TTY_STATE_WAIT_SPACE 2 /*010*/ -#define TTY_STATE_DISPLAY 1 /*001*/ +#define TTY_STATE_WAIT_ENTER 4 /*100*/ +#define TTY_STATE_WAIT_SPACE 2 /*010*/ +#define TTY_STATE_DISPLAY 1 /*001*/ struct s_tty; struct s_console; @@ -33,11 +32,11 @@ struct s_console; /* TTY */ typedef struct s_tty { - u32 ibuf[TTY_IN_BYTES]; /* TTY input buffer */ - u32* ibuf_head; /* the next free slot */ - u32* ibuf_tail; /* 缓冲区显示位置指针 */ - u32* ibuf_read; - int ibuf_cnt; /* how many */ + u32 ibuf[TTY_IN_BYTES]; /* TTY input buffer */ + u32 *ibuf_head; /* the next free slot */ + u32 *ibuf_tail; /* 缓冲区显示位置指针 */ + u32 *ibuf_read; + int ibuf_cnt; /* how many */ int ibuf_read_cnt; int status; @@ -46,10 +45,11 @@ typedef struct s_tty int mouse_X; int mouse_Y; - struct s_console * console; -}TTY; + struct s_console *console; +} TTY; -typedef struct MouseState{ +typedef struct MouseState +{ u8 mouse_lb; u8 mouse_mb; u8 mouse_rb; @@ -58,21 +58,35 @@ typedef struct MouseState{ int mouse_y; } MouseState; -typedef struct n_tty { +typedef struct n_tty +{ int driver_type; // 1-vga&kbd; 2-serial MouseState mouse; - void* input_buf; - void* output_buf; + void *input_buf; + void *output_buf; } NTTY; -typedef struct vga_buf { - void* buf; // 2d array, screen size, for text mode it's [maxline][80] - int max_line; // to support scroll, max line should be a lot more than screen +enum CSI_state +{ + CSI_ESC, + CSI_BRACKET, + CSI_PARAM1, + CSI_PARAM2 +}; +typedef struct vga_buf +{ + void *buf; // 2d array, screen size, for text mode it's [maxline][80] + int max_line; // to support scroll, max line should be a lot more than screen int scr_top_line; // the index in buf of top line on screen; int scr_cur_line; // the index in buf of the cursor's line - int head_line; // for circular buffer use + int head_line; // for circular buffer use int cur_row; int cur_col; // cursor position, on screen + + enum CSI_state CSI; + i16 param1; // the first param of CSI + i16 param2; // the second of CSI + u16 color; } vga_buf; typedef struct keyboard_buf { @@ -86,9 +100,9 @@ typedef struct keyboard_buf { #include "console.h" void select_console(int nr_console); -void init_screen(TTY* tty); -void out_char(CONSOLE* con, char ch); -int is_current_console(CONSOLE* con); +void init_screen(TTY *tty); +void out_char(CONSOLE *con, char ch); +int is_current_console(CONSOLE *con); int init_serial(); char read_serial(); @@ -106,6 +120,9 @@ int ps2_tty_read(NTTY* tty, u8* buf, int nr); #define NEXT(x, _max) (((x) + 1) % (_max)) #define LAST(x, _max) (((x) - 1) >= 0 ? ((x) - 1) % (_max) : (_max) - 1) #define DEBUGNEW +#define FOREGROUND(color) ((u16)(color & 0xf) << 8) +#define BACKGROUND(color) ((u16)(color & 0xf) << 12) + extern int cur_ntty; extern NTTY ntty_table[3]; #endif /* _ORANGES_TTY_H_ */ \ No newline at end of file diff --git a/kernel/console.c b/kernel/console.c index 2294f5e..1682a7f 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -13,56 +13,58 @@ #include "x86.h" /* local routines */ -static void set_cursor(unsigned int position); -static void set_video_start_addr(u32 addr); -static void flush(CONSOLE* con); -static void w_copy(unsigned int dst, const unsigned int src, int size); -static void clear_screen(int pos, int len); -void scroll_screen(CONSOLE* con, int dir); +static void set_cursor(unsigned int position); +static void set_video_start_addr(u32 addr); +static void flush(CONSOLE *con); +static void w_copy(unsigned int dst, const unsigned int src, int size); +static void clear_screen(int pos, int len); +void scroll_screen(CONSOLE *con, int dir); /***************************************************************************** * init_screen *****************************************************************************/ /** * Initialize the console of a certain tty. - * + * * @param tty Whose console is to be initialized. *****************************************************************************/ -void init_screen(TTY* tty) +void init_screen(TTY *tty) { int nr_tty = tty - tty_table; - + tty->console = console_table + nr_tty; - /* + /* * NOTE: * variables related to `position' and `size' below are * in WORDs, but not in BYTEs. */ int v_mem_size = V_MEM_SIZE >> 1; /* size of Video Memory */ - int size_per_con = (v_mem_size / NR_CONSOLES)/80*80; + int size_per_con = (v_mem_size / NR_CONSOLES) / 80 * 80; tty->console->orig = nr_tty * size_per_con; tty->console->con_size = size_per_con / SCR_WIDTH * SCR_WIDTH; tty->console->cursor = tty->console->crtc_start = tty->console->orig; tty->console->is_full = 0; tty->console->current_line = 0; - if(nr_tty==0){ + if (nr_tty == 0) + { tty->console->cursor = disp_pos / 2; } const char prompt[] = "[TTY #?]\n"; - const char * p = prompt; - for (; *p; p++){ + const char *p = prompt; + for (; *p; p++) + { out_char(tty->console, *p == '?' ? nr_tty + '0' : *p); } set_cursor(tty->console->cursor); } - -static inline void write_char_c(int pos, char ch) { - u16* pch = (u16*)K_PHY2LIN(V_MEM_BASE + pos);\ +static inline void write_char_c(int pos, char ch) +{ + u16 *pch = (u16 *)K_PHY2LIN(V_MEM_BASE + pos); *pch = (0x0f << 8) | ch; } @@ -71,28 +73,30 @@ static inline void write_char_c(int pos, char ch) { *****************************************************************************/ /** * Print a char in a certain console. - * + * * @param con The console to which the char is printed. * @param ch The char to print. *****************************************************************************/ -void out_char(CONSOLE* con, char ch) +void out_char(CONSOLE *con, char ch) { disable_int(); int cursor_x = (con->cursor - con->orig) % SCR_WIDTH; int cursor_y = (con->cursor - con->orig) / SCR_WIDTH; - - switch(ch) { + + switch (ch) + { case '\n': con->cursor = con->orig + SCR_WIDTH * (cursor_y + 1); break; case '\b': - if (con->cursor > con->orig) { + if (con->cursor > con->orig) + { con->cursor--; //*(pch - 2) = ' '; //*(pch - 1) = DEFAULT_CHAR_COLOR; - disp_pos = con->cursor*2; + disp_pos = con->cursor * 2; // write_char(' '); write_char_c(disp_pos, ' '); } @@ -100,17 +104,16 @@ void out_char(CONSOLE* con, char ch) default: //*pch++ = ch; //*pch++ = DEFAULT_CHAR_COLOR; - disp_pos = con->cursor*2; + disp_pos = con->cursor * 2; // write_char(ch); write_char_c(disp_pos, ch); con->cursor++; - + break; } - - - if (con->cursor - con->orig >= con->con_size) { + if (con->cursor - con->orig >= con->con_size) + { cursor_x = (con->cursor - con->orig) % SCR_WIDTH; cursor_y = (con->cursor - con->orig) / SCR_WIDTH; int cp_orig = con->orig + (cursor_y + 1) * SCR_WIDTH - SCR_SIZE; @@ -122,10 +125,11 @@ void out_char(CONSOLE* con, char ch) con->is_full = 1; } - //assert(con->cursor - con->orig < con->con_size); + // assert(con->cursor - con->orig < con->con_size); while (con->cursor >= con->crtc_start + SCR_SIZE || - con->cursor < con->crtc_start) { + con->cursor < con->crtc_start) + { scroll_screen(con, SCR_UP); clear_screen(con->cursor, SCR_WIDTH); @@ -136,48 +140,46 @@ void out_char(CONSOLE* con, char ch) enable_int(); } - /***************************************************************************** * clear_screen *****************************************************************************/ /** * Write whitespaces to the screen. - * + * * @param pos Write from here. * @param len How many whitespaces will be written. *****************************************************************************/ static void clear_screen(int pos, int len) { - u8 * pch = (u8*)K_PHY2LIN(V_MEM_BASE + pos * 2); - while (--len >= 0) { + u8 *pch = (u8 *)K_PHY2LIN(V_MEM_BASE + pos * 2); + while (--len >= 0) + { *pch++ = ' '; - *pch++ = DEFAULT_CHAR_COLOR; + *pch++ = (u8)(DEFAULT_CHAR_COLOR >> 8); } } - /***************************************************************************** * is_current_console *****************************************************************************/ /** * Uses `nr_current_console' to determine if a console is the current one. - * + * * @param con Ptr to console. - * + * * @return TRUE if con is the current console. *****************************************************************************/ -int is_current_console(CONSOLE* con) +int is_current_console(CONSOLE *con) { return (con == &console_table[current_console]); } - /***************************************************************************** * set_cursor *****************************************************************************/ /** * Display the cursor by setting CRTC (6845 compatible) registers. - * + * * @param position Position of the cursor based on the beginning of the video * memory. Note that it counts in WORDs, not in BYTEs. *****************************************************************************/ @@ -191,13 +193,12 @@ static void set_cursor(unsigned int position) enable_int(); } - /***************************************************************************** * set_video_start_addr *****************************************************************************/ /** * Routine for hardware screen scrolling. - * + * * @param addr Offset in the video memory. *****************************************************************************/ static void set_video_start_addr(u32 addr) @@ -210,23 +211,22 @@ static void set_video_start_addr(u32 addr) enable_int(); } - /***************************************************************************** * select_console *****************************************************************************/ /** * Select a console as the current. - * + * * @param nr_console Console nr, range in [0, NR_CONSOLES-1]. *****************************************************************************/ void select_console(int nr_console) { - if ((nr_console < 0) || (nr_console >= NR_CONSOLES)) return; + if ((nr_console < 0) || (nr_console >= NR_CONSOLES)) + return; flush(&console_table[current_console = nr_console]); } - /***************************************************************************** * scroll_screen *****************************************************************************/ @@ -241,73 +241,83 @@ void select_console(int nr_console) * When there is no line below the bottom of the screen, scrolling UP takes no * effects; when there is no line above the top of the screen, scrolling DOWN * takes no effects. - * + * * @param con The console whose screen is to be scrolled. * @param dir SCR_UP : scroll the screen upwards; * SCR_DN : scroll the screen downwards *****************************************************************************/ -void scroll_screen(CONSOLE* con, int dir) +void scroll_screen(CONSOLE *con, int dir) { /* * variables below are all in-console-offsets (based on con->orig) */ - int oldest; /* addr of the oldest available line in the console */ - int newest; /* .... .. ... latest ......... .... .. ... ....... */ - int scr_top;/* position of the top of current screen */ + int oldest; /* addr of the oldest available line in the console */ + int newest; /* .... .. ... latest ......... .... .. ... ....... */ + int scr_top; /* position of the top of current screen */ newest = (con->cursor - con->orig) / SCR_WIDTH * SCR_WIDTH; oldest = con->is_full ? (newest + SCR_WIDTH) % con->con_size : 0; scr_top = con->crtc_start - con->orig; - if (dir == SCR_DN) { - if (!con->is_full && scr_top > 0) { + if (dir == SCR_DN) + { + if (!con->is_full && scr_top > 0) + { con->crtc_start -= SCR_WIDTH; } - else if (con->is_full && scr_top != oldest) { - if (con->cursor - con->orig >= con->con_size - SCR_SIZE) { + else if (con->is_full && scr_top != oldest) + { + if (con->cursor - con->orig >= con->con_size - SCR_SIZE) + { if (con->crtc_start != con->orig) con->crtc_start -= SCR_WIDTH; } - else if (con->crtc_start == con->orig) { + else if (con->crtc_start == con->orig) + { scr_top = con->con_size - SCR_SIZE; con->crtc_start = con->orig + scr_top; } - else { + else + { con->crtc_start -= SCR_WIDTH; } } } - else if (dir == SCR_UP) { - if (!con->is_full && newest >= scr_top + SCR_SIZE) { + else if (dir == SCR_UP) + { + if (!con->is_full && newest >= scr_top + SCR_SIZE) + { con->crtc_start += SCR_WIDTH; } - else if (con->is_full && scr_top + SCR_SIZE - SCR_WIDTH != newest) { + else if (con->is_full && scr_top + SCR_SIZE - SCR_WIDTH != newest) + { if (scr_top + SCR_SIZE == con->con_size) con->crtc_start = con->orig; else con->crtc_start += SCR_WIDTH; } } - else { - //assert(dir == SCR_DN || dir == SCR_UP); + else + { + // assert(dir == SCR_DN || dir == SCR_UP); } flush(con); } - /***************************************************************************** * flush *****************************************************************************/ /** * Set the cursor and starting address of a console by writing the * CRT Controller Registers. - * + * * @param con The console to be set. *****************************************************************************/ -static void flush(CONSOLE* con) +static void flush(CONSOLE *con) { - if (is_current_console(con)) { + if (is_current_console(con)) + { set_cursor(con->cursor); set_video_start_addr(con->crtc_start); } @@ -321,14 +331,14 @@ static void flush(CONSOLE* con) * * Note that the addresses of dst and src are not pointers, but integers, 'coz * in most cases we pass integers into it as parameters. - * + * * @param dst Addr of destination. * @param src Addr of source. * @param size How many words will be copied. *****************************************************************************/ -static void w_copy(unsigned int dst, const unsigned int src, int size) +static void w_copy(unsigned int dst, const unsigned int src, int size) { - memcpy((void*)(V_MEM_BASE + (dst << 1)), - (void*)(V_MEM_BASE + (src << 1)), - size << 1); + memcpy((void *)(V_MEM_BASE + (dst << 1)), + (void *)(V_MEM_BASE + (src << 1)), + size << 1); } \ No newline at end of file diff --git a/kernel/fs.c b/kernel/fs.c index b85dcfa..11d341b 100644 --- a/kernel/fs.c +++ b/kernel/fs.c @@ -14,24 +14,24 @@ #include "stdio.h" #include "assert.h" -//added by xw, 18/8/28 +// added by xw, 18/8/28 /* data */ -static struct inode* root_inode; +static struct inode *root_inode; -//static struct file_desc f_desc_table[NR_FILE_DESC]; //deleted by mingxuan 2020-10-30 -extern struct file_desc f_desc_table[NR_FILE_DESC]; //modified by mingxuan 2020-10-30 +// static struct file_desc f_desc_table[NR_FILE_DESC]; //deleted by mingxuan 2020-10-30 +extern struct file_desc f_desc_table[NR_FILE_DESC]; // modified by mingxuan 2020-10-30 -static struct inode inode_table[NR_INODE]; +static struct inode inode_table[NR_INODE]; -//static struct super_block super_block[NR_SUPER_BLOCK]; //deleted by mingxuan 2020-10-30 -extern struct super_block super_block[NR_SUPER_BLOCK]; //modified by mingxuan 2020-10-30 +// static struct super_block super_block[NR_SUPER_BLOCK]; //deleted by mingxuan 2020-10-30 +extern struct super_block super_block[NR_SUPER_BLOCK]; // modified by mingxuan 2020-10-30 /* functions */ static void mkfs(); -//static void read_super_block(int dev); - void read_super_block(int dev); // modified by mingxuan 2020-10-30 -//static struct super_block* get_super_block(int dev); - struct super_block* get_super_block(int dev); // modified by mingxuan 2020-10-30 +// static void read_super_block(int dev); +void read_super_block(int dev); // modified by mingxuan 2020-10-30 + // static struct super_block* get_super_block(int dev); +struct super_block *get_super_block(int dev); // modified by mingxuan 2020-10-30 static int do_open(MESSAGE *fs_msg); static int do_close(int fd); @@ -39,79 +39,79 @@ static int do_lseek(MESSAGE *fs_msg); static int do_rdwt(MESSAGE *fs_msg); static int do_unlink(MESSAGE *fs_msg); -int real_open(const char *pathname, int flags); //modified by mingxuan 2019-5-17 -int real_close(int fd); //modified by mingxuan 2019-5-17 -int real_read(int fd, void *buf, int count); //注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 -int real_write(int fd, const void *buf, int count); //注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 -int real_unlink(const char *pathname); //modified by mingxuan 2019-5-17 -int real_lseek(int fd, int offset, int whence); //modified by mingxuan 2019-5-17 +int real_open(const char *pathname, int flags); // modified by mingxuan 2019-5-17 +int real_close(int fd); // modified by mingxuan 2019-5-17 +int real_read(int fd, void *buf, int count); // 注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 +int real_write(int fd, const void *buf, int count); // 注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 +int real_unlink(const char *pathname); // modified by mingxuan 2019-5-17 +int real_lseek(int fd, int offset, int whence); // modified by mingxuan 2019-5-17 -static int rw_sector(int io_type, int dev, u64 pos, int bytes, int proc_nr, void* buf); -static int rw_sector_sched(int io_type, int dev, int pos, int bytes, int proc_nr, void* buf); +static int rw_sector(int io_type, int dev, u64 pos, int bytes, int proc_nr, void *buf); +static int rw_sector_sched(int io_type, int dev, int pos, int bytes, int proc_nr, void *buf); -static int strip_path(char * filename, const char * pathname, struct inode** ppinode); +static int strip_path(char *filename, const char *pathname, struct inode **ppinode); static int search_file(char *path); -static struct inode* create_file(char *path, int flags); -static struct inode* get_inode(int dev, int num); -static struct inode* get_inode_sched(int dev, int num); -static struct inode* new_inode(int dev, int inode_nr, int start_sect); +static struct inode *create_file(char *path, int flags); +static struct inode *get_inode(int dev, int num); +static struct inode *get_inode_sched(int dev, int num); +static struct inode *new_inode(int dev, int inode_nr, int start_sect); static void put_inode(struct inode *pinode); static void sync_inode(struct inode *p); -static void new_dir_entry(struct inode *dir_inode,int inode_nr,char *filename); +static void new_dir_entry(struct inode *dir_inode, int inode_nr, char *filename); static int alloc_imap_bit(int dev); static int alloc_smap_bit(int dev, int nr_sects_to_alloc); static int memcmp(const void *s1, const void *s2, int n); //~xw -int get_fs_dev(int drive, int fs_type); // added by mingxuan 2020-10-27 - +int get_fs_dev(int drive, int fs_type); // added by mingxuan 2020-10-27 // added by mingxuan 2020-10-27 int get_fs_dev(int drive, int fs_type) { - int i=0; - for(i=0; i < NR_PRIM_PER_DRIVE; i++) + int i = 0; + for (i = 0; i < NR_PRIM_PER_DRIVE; i++) { - if(hd_info[drive].primary[i].fs_type == fs_type) - return ((DEV_HD << MAJOR_SHIFT) | i); + if (hd_info[drive].primary[i].fs_type == fs_type) + return ((DEV_HD << MAJOR_SHIFT) | i); } - //added by mingxuan 2020-10-29 - for(i=0; i < NR_SUB_PER_DRIVE; i++) + // added by mingxuan 2020-10-29 + for (i = 0; i < NR_SUB_PER_DRIVE; i++) { - if(hd_info[drive].logical[i].fs_type == fs_type) - return ((DEV_HD << MAJOR_SHIFT) | (i + MINOR_hd1a)); // logic的下标i加上hd1a才是该逻辑分区的次设备号 + if (hd_info[drive].logical[i].fs_type == fs_type) + return ((DEV_HD << MAJOR_SHIFT) | (i + MINOR_hd1a)); // logic的下标i加上hd1a才是该逻辑分区的次设备号 } return 0; } /// zcr added -void init_fs() +void init_fs() { kprintf("Initializing file system... "); - + int i; for (i = 0; i < NR_INODE; i++) memset(&inode_table[i], 0, sizeof(struct inode)); - struct super_block * sb = super_block; //deleted by mingxuan 2020-10-30 - - int orange_dev = get_fs_dev(PRIMARY_MASTER, ORANGE_TYPE); //added by mingxuan 2020-10-27 + struct super_block *sb = super_block; // deleted by mingxuan 2020-10-30 + + int orange_dev = get_fs_dev(PRIMARY_MASTER, ORANGE_TYPE); // added by mingxuan 2020-10-27 /* load super block of ROOT */ - read_super_block(orange_dev); // modified by mingxuan 2020-10-27 - sb = get_super_block(orange_dev); // modified by mingxuan 2020-10-27 + read_super_block(orange_dev); // modified by mingxuan 2020-10-27 + sb = get_super_block(orange_dev); // modified by mingxuan 2020-10-27 kprintf("Superblock Address:0x%x \n", sb); - if(sb->magic != MAGIC_V1) { //deleted by mingxuan 2019-5-20 + if (sb->magic != MAGIC_V1) + { // deleted by mingxuan 2019-5-20 mkfs(); kprintf("Make file system Done.\n"); - read_super_block(orange_dev); // modified by mingxuan 2020-10-27 + read_super_block(orange_dev); // modified by mingxuan 2020-10-27 } - root_inode = get_inode(orange_dev, ROOT_INODE); // modified by mingxuan 2020-10-27 + root_inode = get_inode(orange_dev, ROOT_INODE); // modified by mingxuan 2020-10-27 } /***************************************************************************** @@ -133,19 +133,19 @@ static void mkfs() int bits_per_sect = SECTOR_SIZE * 8; /* 8 bits per byte */ - //local array, to substitute global fsbuf. added by xw, 18/12/27 + // local array, to substitute global fsbuf. added by xw, 18/12/27 char fsbuf[SECTOR_SIZE]; - int orange_dev = get_fs_dev(PRIMARY_MASTER, ORANGE_TYPE); //added by mingxuan 2020-10-27 + int orange_dev = get_fs_dev(PRIMARY_MASTER, ORANGE_TYPE); // added by mingxuan 2020-10-27 /* get the geometry of ROOTDEV */ struct part_info geo; - driver_msg.type = DEV_IOCTL; - //driver_msg.DEVICE = MINOR(ROOT_DEV); // deleted by mingxuan 2020-10-27 - driver_msg.DEVICE = MINOR(orange_dev); // modified by mingxuan 2020-10-27 - driver_msg.REQUEST = DIOCTL_GET_GEO; - driver_msg.BUF = &geo; - driver_msg.PROC_NR = proc2pid(p_proc_current); + driver_msg.type = DEV_IOCTL; + // driver_msg.DEVICE = MINOR(ROOT_DEV); // deleted by mingxuan 2020-10-27 + driver_msg.DEVICE = MINOR(orange_dev); // modified by mingxuan 2020-10-27 + driver_msg.REQUEST = DIOCTL_GET_GEO; + driver_msg.BUF = &geo; + driver_msg.PROC_NR = proc2pid(p_proc_current); // assert(dd_map[MAJOR(ROOT_DEV)].driver_nr != INVALID_DRIVER); // send_recv(BOTH, dd_map[MAJOR(ROOT_DEV)].driver_nr, &driver_msg); hd_ioctl(&driver_msg); @@ -156,34 +156,34 @@ static void mkfs() /* super block */ /************************/ struct super_block sb; - sb.magic = MAGIC_V1; - sb.nr_inodes = bits_per_sect; + sb.magic = MAGIC_V1; + sb.nr_inodes = bits_per_sect; sb.nr_inode_sects = sb.nr_inodes * INODE_SIZE / SECTOR_SIZE; - sb.nr_sects = geo.size; /* partition size in sector */ - sb.nr_imap_sects = 1; - sb.nr_smap_sects = sb.nr_sects / bits_per_sect + 1; - sb.n_1st_sect = 1 + 1 + /* boot sector & super block */ - sb.nr_imap_sects + sb.nr_smap_sects + sb.nr_inode_sects; - sb.root_inode = ROOT_INODE; - sb.inode_size = INODE_SIZE; - + sb.nr_sects = geo.size; /* partition size in sector */ + sb.nr_imap_sects = 1; + sb.nr_smap_sects = sb.nr_sects / bits_per_sect + 1; + sb.n_1st_sect = 1 + 1 + /* boot sector & super block */ + sb.nr_imap_sects + sb.nr_smap_sects + sb.nr_inode_sects; + sb.root_inode = ROOT_INODE; + sb.inode_size = INODE_SIZE; + struct inode x; - sb.inode_isize_off= (int)&x.i_size - (int)&x; - sb.inode_start_off= (int)&x.i_start_sect - (int)&x; - sb.dir_ent_size = DIR_ENTRY_SIZE; - + sb.inode_isize_off = (int)&x.i_size - (int)&x; + sb.inode_start_off = (int)&x.i_start_sect - (int)&x; + sb.dir_ent_size = DIR_ENTRY_SIZE; + struct dir_entry de; sb.dir_ent_inode_off = (int)&de.inode_nr - (int)&de; sb.dir_ent_fname_off = (int)&de.name - (int)&de; - sb.sb_dev = orange_dev; //added by mingxuan 2020-10-30 - sb.fs_type = ORANGE_TYPE; //added by mingxuan 2020-10-30 + sb.sb_dev = orange_dev; // added by mingxuan 2020-10-30 + sb.fs_type = ORANGE_TYPE; // added by mingxuan 2020-10-30 memset(fsbuf, 0x90, SECTOR_SIZE); memcpy(fsbuf, &sb, SUPER_BLOCK_SIZE); /* write the super block */ - WR_SECT(orange_dev, 1, fsbuf); // modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 1, fsbuf); // modified by mingxuan 2020-10-27 kprintf("devbase:0x%x00", (geo.base + 0) * 2); kprintf(" sb:0x%x00", (geo.base + 1) * 2); @@ -196,11 +196,10 @@ static void mkfs() /* inode map */ /************************/ memset(fsbuf, 0, SECTOR_SIZE); - for (i = 0; i < (NR_CONSOLES + 3); i++) //modified by mingxuan 2019-5-22 + for (i = 0; i < (NR_CONSOLES + 3); i++) // modified by mingxuan 2019-5-22 fsbuf[0] |= 1 << i; - - WR_SECT(orange_dev, 2, fsbuf); //modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 2, fsbuf); // modified by mingxuan 2020-10-27 /************************/ /* secter map */ @@ -213,56 +212,59 @@ static void mkfs() for (j = 0; j < nr_sects % 8; j++) fsbuf[i] |= (1 << j); - WR_SECT(orange_dev, 2 + sb.nr_imap_sects, fsbuf); //modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 2 + sb.nr_imap_sects, fsbuf); // modified by mingxuan 2020-10-27 /* zeromemory the rest sector-map */ memset(fsbuf, 0, SECTOR_SIZE); for (i = 1; i < sb.nr_smap_sects; i++) - WR_SECT(orange_dev, 2 + sb.nr_imap_sects + i, fsbuf); //modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 2 + sb.nr_imap_sects + i, fsbuf); // modified by mingxuan 2020-10-27 /* app.tar */ - //added by mingxuan 2019-5-19 + // added by mingxuan 2019-5-19 int bit_offset = INSTALL_START_SECTOR - - sb.n_1st_sect + 1; /* sect M <-> bit (M - sb.n_1stsect + 1) */ + sb.n_1st_sect + 1; /* sect M <-> bit (M - sb.n_1stsect + 1) */ int bit_off_in_sect = bit_offset % (SECTOR_SIZE * 8); int bit_left = INSTALL_NR_SECTORS; int cur_sect = bit_offset / (SECTOR_SIZE * 8); - RD_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf);//modified by mingxuan 2020-10-27 - while (bit_left) { + RD_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); // modified by mingxuan 2020-10-27 + while (bit_left) + { int byte_off = bit_off_in_sect / 8; /* this line is ineffecient in a loop, but I don't care */ fsbuf[byte_off] |= 1 << (bit_off_in_sect % 8); bit_left--; bit_off_in_sect++; - if (bit_off_in_sect == (SECTOR_SIZE * 8)) { - WR_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); //modified by mingxuan 2020-10-27 + if (bit_off_in_sect == (SECTOR_SIZE * 8)) + { + WR_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); // modified by mingxuan 2020-10-27 cur_sect++; - RD_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); //modified by mingxuan 2020-10-27 + RD_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); // modified by mingxuan 2020-10-27 bit_off_in_sect = 0; } } - WR_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); //modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 2 + sb.nr_imap_sects + cur_sect, fsbuf); // modified by mingxuan 2020-10-27 /************************/ /* inodes */ /************************/ /* inode of `/' */ memset(fsbuf, 0, SECTOR_SIZE); - struct inode * pi = (struct inode*)fsbuf; + struct inode *pi = (struct inode *)fsbuf; pi->i_mode = I_DIRECTORY; - //modified by mingxuan 2019-5-21 + // modified by mingxuan 2019-5-21 pi->i_size = DIR_ENTRY_SIZE * 5; /* 5 files: (预定义5个文件) - * `.', - * `dev_tty0', `dev_tty1', `dev_tty2','app.tar' - */ + * `.', + * `dev_tty0', `dev_tty1', `dev_tty2','app.tar' + */ pi->i_start_sect = sb.n_1st_sect; pi->i_nr_sects = NR_DEFAULT_FILE_SECTS; - + /* inode of `/dev_tty0~2' */ - for (i = 0; i < NR_CONSOLES; i++) { - pi = (struct inode*)(fsbuf + (INODE_SIZE * (i + 1))); + for (i = 0; i < NR_CONSOLES; i++) + { + pi = (struct inode *)(fsbuf + (INODE_SIZE * (i + 1))); pi->i_mode = I_CHAR_SPECIAL; pi->i_size = 0; pi->i_start_sect = MAKE_DEV(DEV_CHAR_TTY, i); @@ -270,46 +272,48 @@ static void mkfs() } /* inode of /app.tar */ - //added by mingxuan 2019-5-19 - pi = (struct inode*)(fsbuf + (INODE_SIZE * (NR_CONSOLES + 1))); + // added by mingxuan 2019-5-19 + pi = (struct inode *)(fsbuf + (INODE_SIZE * (NR_CONSOLES + 1))); pi->i_mode = I_REGULAR; pi->i_size = INSTALL_NR_SECTORS * SECTOR_SIZE; pi->i_start_sect = INSTALL_START_SECTOR; pi->i_nr_sects = INSTALL_NR_SECTORS; - WR_SECT(orange_dev, 2 + sb.nr_imap_sects + sb.nr_smap_sects, fsbuf); //modified by mingxuan 2020-10-27 + WR_SECT(orange_dev, 2 + sb.nr_imap_sects + sb.nr_smap_sects, fsbuf); // modified by mingxuan 2020-10-27 /************************/ /* `/' */ /************************/ memset(fsbuf, 0, SECTOR_SIZE); - struct dir_entry * pde = (struct dir_entry *)fsbuf; + struct dir_entry *pde = (struct dir_entry *)fsbuf; pde->inode_nr = 1; strcpy(pde->name, "."); /* dir entries of `/dev_tty0~2' */ - for (i = 0; i < NR_CONSOLES; i++) { + for (i = 0; i < NR_CONSOLES; i++) + { pde++; pde->inode_nr = i + 2; /* dev_tty0's inode_nr is 2 */ - switch(i) { - case 0: - strcpy(pde->name, "dev_tty0"); - break; - case 1: - strcpy(pde->name, "dev_tty1"); - break; - case 2: - strcpy(pde->name, "dev_tty2"); - break; + switch (i) + { + case 0: + strcpy(pde->name, "dev_tty0"); + break; + case 1: + strcpy(pde->name, "dev_tty1"); + break; + case 2: + strcpy(pde->name, "dev_tty2"); + break; } } - (++pde)->inode_nr = NR_CONSOLES + 2; //added by mingxuan 2019-5-19 - strcpy(pde->name, INSTALL_FILENAME); //added by mingxuan 2019-5-19 + (++pde)->inode_nr = NR_CONSOLES + 2; // added by mingxuan 2019-5-19 + strcpy(pde->name, INSTALL_FILENAME); // added by mingxuan 2019-5-19 - //WR_SECT(ROOT_DEV, sb.n_1st_sect, fsbuf); //modified by xw, 18/12/27 //deleted by mingxuan 2020-10-27 - WR_SECT(orange_dev, sb.n_1st_sect, fsbuf); //modified by mingxuan 2020-10-27 + // WR_SECT(ROOT_DEV, sb.n_1st_sect, fsbuf); //modified by xw, 18/12/27 //deleted by mingxuan 2020-10-27 + WR_SECT(orange_dev, sb.n_1st_sect, fsbuf); // modified by mingxuan 2020-10-27 } /***************************************************************************** @@ -317,45 +321,45 @@ static void mkfs() *****************************************************************************/ /** * R/W a sector via messaging with the corresponding driver. - * + * * @param io_type DEV_READ or DEV_WRITE * @param dev device nr * @param pos Byte offset from/to where to r/w. * @param bytes r/w count in bytes. * @param proc_nr To whom the buffer belongs. * @param buf r/w buffer. - * + * * @return Zero if success. *****************************************************************************/ /// zcr: change the "u64 pos" to "int pos" -static int rw_sector(int io_type, int dev, u64 pos, int bytes, int proc_nr, void* buf) +static int rw_sector(int io_type, int dev, u64 pos, int bytes, int proc_nr, void *buf) { MESSAGE driver_msg; - - driver_msg.type = io_type; - driver_msg.DEVICE = MINOR(dev); - driver_msg.POSITION = pos; - driver_msg.CNT = bytes; /// hu is: 512 - driver_msg.PROC_NR = proc_nr; - driver_msg.BUF = buf; + + driver_msg.type = io_type; + driver_msg.DEVICE = MINOR(dev); + driver_msg.POSITION = pos; + driver_msg.CNT = bytes; /// hu is: 512 + driver_msg.PROC_NR = proc_nr; + driver_msg.BUF = buf; hd_rdwt(&driver_msg); return 0; } -//added by xw, 18/8/27 -static int rw_sector_sched(int io_type, int dev, int pos, int bytes, int proc_nr, void* buf) +// added by xw, 18/8/27 +static int rw_sector_sched(int io_type, int dev, int pos, int bytes, int proc_nr, void *buf) { MESSAGE driver_msg; - - driver_msg.type = io_type; - driver_msg.DEVICE = MINOR(dev); - driver_msg.POSITION = pos; - driver_msg.CNT = bytes; /// hu is: 512 - driver_msg.PROC_NR = proc_nr; - driver_msg.BUF = buf; - + driver_msg.type = io_type; + driver_msg.DEVICE = MINOR(dev); + + driver_msg.POSITION = pos; + driver_msg.CNT = bytes; /// hu is: 512 + driver_msg.PROC_NR = proc_nr; + driver_msg.BUF = buf; + hd_rdwt_sched(&driver_msg); return 0; } @@ -368,24 +372,24 @@ static int rw_sector_sched(int io_type, int dev, int pos, int bytes, int proc_nr *****************************************************************************/ /** * open/create a file. - * + * * @param pathname The full path of the file to be opened/created. * @param flags O_CREAT, O_RDWR, etc. - * + * * @return File descriptor if successful, otherwise -1. *****************************************************************************/ -//open is a syscall interface now. added by xw, 18/6/18 -// int open(const char *pathname, int flags) -// static int real_open(const char *pathname, int flags) //deleted by mingxuan 2019-5-17 -int real_open(const char *pathname, int flags) //modified by mingxuan 2019-5-17 +// open is a syscall interface now. added by xw, 18/6/18 +// int open(const char *pathname, int flags) +// static int real_open(const char *pathname, int flags) //deleted by mingxuan 2019-5-17 +int real_open(const char *pathname, int flags) // modified by mingxuan 2019-5-17 { - //added by xw, 18/8/27 + // added by xw, 18/8/27 MESSAGE fs_msg; - fs_msg.type = OPEN; - fs_msg.PATHNAME = (void*)pathname; - fs_msg.FLAGS = flags; - fs_msg.NAME_LEN = strlen(pathname); + fs_msg.type = OPEN; + fs_msg.PATHNAME = (void *)pathname; + fs_msg.FLAGS = flags; + fs_msg.NAME_LEN = strlen(pathname); fs_msg.source = proc2pid(p_proc_current); int fd = do_open(&fs_msg); @@ -398,94 +402,107 @@ int real_open(const char *pathname, int flags) //modified by mingxuan 2019-5-17 *****************************************************************************/ /** * Open a file and return the file descriptor. - * + * * @return File descriptor if successful, otherwise a negative error code. *****************************************************************************/ /// zcr modified. static int do_open(MESSAGE *fs_msg) { /*caller_nr is the process number of the */ - int fd = -1; /* return value */ + int fd = -1; /* return value */ char pathname[MAX_PATH]; /* get parameters from the message */ - int flags = fs_msg->FLAGS; /* access mode */ - int name_len = fs_msg->NAME_LEN; /* length of filename */ - int src = fs_msg->source; /* caller proc nr. */ + int flags = fs_msg->FLAGS; /* access mode */ + int name_len = fs_msg->NAME_LEN; /* length of filename */ + int src = fs_msg->source; /* caller proc nr. */ - memcpy((void*)va2la(src, pathname), (void*)va2la(src, fs_msg->PATHNAME), name_len); + memcpy((void *)va2la(src, pathname), (void *)va2la(src, fs_msg->PATHNAME), name_len); pathname[name_len] = 0; /* find a free slot in PROCESS::filp[] */ int i; - for (i = 0; i < NR_FILES; i++) { //modified by mingxuan 2019-5-20 - if (p_proc_current->task.filp[i] == 0) { + for (i = 0; i < NR_FILES; i++) + { // modified by mingxuan 2019-5-20 + if (p_proc_current->task.filp[i] == 0) + { fd = i; break; } } - + // printf("%d\n", fd); assert(0 <= fd && fd < NR_FILES); /* find a free slot in f_desc_table[] */ for (i = 0; i < NR_FILE_DESC; i++) - //modified by mingxuan 2019-5-17 + // modified by mingxuan 2019-5-17 if (f_desc_table[i].flag == 0) break; - + assert(i < NR_FILE_DESC); int inode_nr = search_file(pathname); - struct inode * pin = 0; - if (flags & O_CREAT) { - if (inode_nr) { + struct inode *pin = 0; + if (flags & O_CREAT) + { + if (inode_nr) + { return -1; } - else { + else + { pin = create_file(pathname, flags); } } - else { + else + { char filename[MAX_PATH]; - struct inode * dir_inode; + struct inode *dir_inode; if (strip_path(filename, pathname, &dir_inode) != 0) return -1; - pin = get_inode(dir_inode->i_dev, inode_nr); //modified by mingxuan 2019-5-20 + pin = get_inode(dir_inode->i_dev, inode_nr); // modified by mingxuan 2019-5-20 } - if (pin) { + if (pin) + { /* connects proc with file_descriptor */ p_proc_current->task.filp[fd] = &f_desc_table[i]; - - f_desc_table[i].flag = 1; //added by mingxuan 2019-5-17 + + f_desc_table[i].flag = 1; // added by mingxuan 2019-5-17 /* connects file_descriptor with inode */ - f_desc_table[i].fd_node.fd_inode = pin; //modified by mingxuan 2019-5-17 + f_desc_table[i].fd_node.fd_inode = pin; // modified by mingxuan 2019-5-17 f_desc_table[i].fd_mode = flags; f_desc_table[i].fd_pos = 0; int imode = pin->i_mode & I_TYPE_MASK; - if (imode == I_CHAR_SPECIAL) { + if (imode == I_CHAR_SPECIAL) + { // MESSAGE driver_msg; // int dev = pin->i_start_sect; //? } - else if (imode == I_DIRECTORY) { - if(pin->i_num != ROOT_INODE) { + else if (imode == I_DIRECTORY) + { + if (pin->i_num != ROOT_INODE) + { panic("pin->i_num != ROOT_INODE"); } } - else { - if(pin->i_mode != I_REGULAR) { + else + { + if (pin->i_mode != I_REGULAR) + { panic("Panic: pin->i_mode != I_REGULAR"); } } } - else { + else + { return -1; } @@ -502,15 +519,15 @@ static int do_open(MESSAGE *fs_msg) * @param[in] flags Attribiutes of the new file * * @return Ptr to i-node of the new file if successful, otherwise 0. - * + * * @see open() * @see do_open() *****************************************************************************/ -static struct inode * create_file(char * path, int flags) +static struct inode *create_file(char *path, int flags) { char filename[MAX_PATH]; - struct inode * dir_inode; + struct inode *dir_inode; if (strip_path(filename, path, &dir_inode) != 0) return 0; @@ -533,33 +550,35 @@ static struct inode * create_file(char * path, int flags) *****************************************************************************/ /** * Compare memory areas. - * + * * @param s1 The 1st area. * @param s2 The 2nd area. * @param n The first n bytes will be compared. - * + * * @return an integer less than, equal to, or greater than zero if the first * n bytes of s1 is found, respectively, to be less than, to match, * or be greater than the first n bytes of s2. *****************************************************************************/ -static int memcmp(const void * s1, const void *s2, int n) +static int memcmp(const void *s1, const void *s2, int n) { - if ((s1 == 0) || (s2 == 0)) { /* for robustness */ + if ((s1 == 0) || (s2 == 0)) + { /* for robustness */ return (s1 - s2); } - const char * p1 = (const char *)s1; - const char * p2 = (const char *)s2; + const char *p1 = (const char *)s1; + const char *p2 = (const char *)s2; int i; - for (i = 0; i < n; i++,p1++,p2++) { - if (*p1 != *p2) { + for (i = 0; i < n; i++, p1++, p2++) + { + if (*p1 != *p2) + { return (*p1 - *p2); } } return 0; } - /***************************************************************************** * search_file *****************************************************************************/ @@ -568,21 +587,21 @@ static int memcmp(const void * s1, const void *s2, int n) * * @param[in] path The full path of the file to search. * @return Ptr to the i-node of the file if successful, otherwise zero. - * + * * @see open() * @see do_open() *****************************************************************************/ -static int search_file(char * path) +static int search_file(char *path) { int i, j; char filename[MAX_PATH]; memset(filename, 0, MAX_FILENAME_LEN); - struct inode * dir_inode; + struct inode *dir_inode; if (strip_path(filename, path, &dir_inode) != 0) return 0; - if (filename[0] == 0) /* path: "/" */ + if (filename[0] == 0) /* path: "/" */ return dir_inode->i_num; /** @@ -591,19 +610,21 @@ static int search_file(char * path) int dir_blk0_nr = dir_inode->i_start_sect; int nr_dir_blks = (dir_inode->i_size + SECTOR_SIZE - 1) / SECTOR_SIZE; int nr_dir_entries = - dir_inode->i_size / DIR_ENTRY_SIZE; /** - * including unused slots - * (the file has been deleted - * but the slot is still there) - */ + dir_inode->i_size / DIR_ENTRY_SIZE; /** + * including unused slots + * (the file has been deleted + * but the slot is still there) + */ int m = 0; - struct dir_entry * pde; - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - for (i = 0; i < nr_dir_blks; i++) { - //RD_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 - RD_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by mingxuan 2019-5-20 + struct dir_entry *pde; + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + for (i = 0; i < nr_dir_blks; i++) + { + // RD_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 + RD_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); // modified by mingxuan 2019-5-20 pde = (struct dir_entry *)fsbuf; - for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++,pde++) { + for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++, pde++) + { if (memcmp(filename, pde->name, MAX_FILENAME_LEN) == 0) return pde->inode_nr; if (++m > nr_dir_entries) @@ -643,13 +664,13 @@ static int search_file(char * path) * @param[out] filename The string for the result. * @param[in] pathname The full pathname. * @param[out] ppinode The ptr of the dir's inode will be stored here. - * + * * @return Zero if success, otherwise the pathname is not valid. *****************************************************************************/ -static int strip_path(char * filename, const char * pathname, struct inode** ppinode) +static int strip_path(char *filename, const char *pathname, struct inode **ppinode) { - const char * s = pathname; - char * t = filename; + const char *s = pathname; + char *t = filename; if (s == 0) return -1; @@ -657,7 +678,8 @@ static int strip_path(char * filename, const char * pathname, struct inode** ppi if (*s == '/') s++; - while (*s) { /* check each character */ + while (*s) + { /* check each character */ if (*s == '/') return -1; *t++ = *s++; @@ -678,22 +700,22 @@ static int strip_path(char * filename, const char * pathname, struct inode** ppi /** * Read super block from the given device then write it into a free * super_block[] slot. - * + * * @param dev From which device the super block comes. *****************************************************************************/ -//static void read_super_block(int dev) -void read_super_block(int dev) //modified by mingxuan 2020-10-30 +// static void read_super_block(int dev) +void read_super_block(int dev) // modified by mingxuan 2020-10-30 { int i; MESSAGE driver_msg; - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 - driver_msg.type = DEV_READ; - driver_msg.DEVICE = MINOR(dev); - driver_msg.POSITION = SECTOR_SIZE * 1; - driver_msg.BUF = fsbuf; - driver_msg.CNT = SECTOR_SIZE; - driver_msg.PROC_NR = proc2pid(p_proc_current);///TASK_A + driver_msg.type = DEV_READ; + driver_msg.DEVICE = MINOR(dev); + driver_msg.POSITION = SECTOR_SIZE * 1; + driver_msg.BUF = fsbuf; + driver_msg.CNT = SECTOR_SIZE; + driver_msg.PROC_NR = proc2pid(p_proc_current); /// TASK_A hd_rdwt(&driver_msg); @@ -703,37 +725,35 @@ void read_super_block(int dev) //modified by mingxuan 2020-10-30 if (i == NR_SUPER_BLOCK) panic("Cannot find orange's superblock!"); - struct super_block * psb = (struct super_block *)fsbuf; + struct super_block *psb = (struct super_block *)fsbuf; super_block[i] = *psb; super_block[i].sb_dev = dev; - super_block[i].fs_type = ORANGE_TYPE; //added by mingxuan 2020-10-30 + super_block[i].fs_type = ORANGE_TYPE; // added by mingxuan 2020-10-30 } - /***************************************************************************** * get_super_block *****************************************************************************/ /** * Get the super block from super_block[]. - * + * * @param dev Device nr. - * + * * @return Super block ptr. *****************************************************************************/ -struct super_block * get_super_block(int dev) //modified by mingxuan 2020-10-30 +struct super_block *get_super_block(int dev) // modified by mingxuan 2020-10-30 { - struct super_block * sb = super_block; - for (; sb < &super_block[NR_SUPER_BLOCK]; sb++){ + struct super_block *sb = super_block; + for (; sb < &super_block[NR_SUPER_BLOCK]; sb++) + { if (sb->sb_dev == dev) return sb; } return 0; } - - /***************************************************************************** * get_inode *****************************************************************************/ @@ -741,29 +761,33 @@ struct super_block * get_super_block(int dev) //modified by mingxuan 2020-10-30 * Get the inode ptr of given inode nr. A cache -- inode_table[] -- is * maintained to make things faster. If the inode requested is already there, * just return it. Otherwise the inode will be read from the disk. - * + * * @param dev Device nr. * @param num I-node nr. - * + * * @return The inode ptr requested. *****************************************************************************/ -static struct inode * get_inode(int dev, int num) +static struct inode *get_inode(int dev, int num) { if (num == 0) return 0; - struct inode * p; - struct inode * q = 0; - for (p = &inode_table[0]; p < &inode_table[NR_INODE]; p++) { - if (p->i_cnt) { /* not a free slot */ - if ((p->i_dev == dev) && (p->i_num == num)) { + struct inode *p; + struct inode *q = 0; + for (p = &inode_table[0]; p < &inode_table[NR_INODE]; p++) + { + if (p->i_cnt) + { /* not a free slot */ + if ((p->i_dev == dev) && (p->i_num == num)) + { /* this is the inode we want */ p->i_cnt++; return p; } } - else { /* a free slot */ - if (!q) /* q hasn't been assigned yet */ + else + { /* a free slot */ + if (!q) /* q hasn't been assigned yet */ q = p; /* q <- the 1st free slot */ } } @@ -775,14 +799,13 @@ static struct inode * get_inode(int dev, int num) q->i_num = num; q->i_cnt = 1; - struct super_block * sb = get_super_block(dev); + struct super_block *sb = get_super_block(dev); int blk_nr = 1 + 1 + sb->nr_imap_sects + sb->nr_smap_sects + ((num - 1) / (SECTOR_SIZE / INODE_SIZE)); - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - RD_SECT(dev, blk_nr, fsbuf); //added by xw, 18/12/27 - struct inode * pinode = - (struct inode*)((u8*)fsbuf + - ((num - 1 ) % (SECTOR_SIZE / INODE_SIZE)) - * INODE_SIZE); + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + RD_SECT(dev, blk_nr, fsbuf); // added by xw, 18/12/27 + struct inode *pinode = + (struct inode *)((u8 *)fsbuf + + ((num - 1) % (SECTOR_SIZE / INODE_SIZE)) * INODE_SIZE); q->i_mode = pinode->i_mode; q->i_size = pinode->i_size; q->i_start_sect = pinode->i_start_sect; @@ -790,24 +813,28 @@ static struct inode * get_inode(int dev, int num) return q; } -//added by xw, 18/8/27 -static struct inode * get_inode_sched(int dev, int num) +// added by xw, 18/8/27 +static struct inode *get_inode_sched(int dev, int num) { if (num == 0) return 0; - struct inode * p; - struct inode * q = 0; - for (p = &inode_table[0]; p < &inode_table[NR_INODE]; p++) { - if (p->i_cnt) { /* not a free slot */ - if ((p->i_dev == dev) && (p->i_num == num)) { + struct inode *p; + struct inode *q = 0; + for (p = &inode_table[0]; p < &inode_table[NR_INODE]; p++) + { + if (p->i_cnt) + { /* not a free slot */ + if ((p->i_dev == dev) && (p->i_num == num)) + { /* this is the inode we want */ p->i_cnt++; return p; } } - else { /* a free slot */ - if (!q) /* q hasn't been assigned yet */ + else + { /* a free slot */ + if (!q) /* q hasn't been assigned yet */ q = p; /* q <- the 1st free slot */ } } @@ -819,14 +846,13 @@ static struct inode * get_inode_sched(int dev, int num) q->i_num = num; q->i_cnt = 1; - struct super_block * sb = get_super_block(dev); + struct super_block *sb = get_super_block(dev); int blk_nr = 1 + 1 + sb->nr_imap_sects + sb->nr_smap_sects + ((num - 1) / (SECTOR_SIZE / INODE_SIZE)); - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - RD_SECT_SCHED(dev, blk_nr, fsbuf); //added by xw, 18/12/27 - struct inode * pinode = - (struct inode*)((u8*)fsbuf + - ((num - 1 ) % (SECTOR_SIZE / INODE_SIZE)) - * INODE_SIZE); + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + RD_SECT_SCHED(dev, blk_nr, fsbuf); // added by xw, 18/12/27 + struct inode *pinode = + (struct inode *)((u8 *)fsbuf + + ((num - 1) % (SECTOR_SIZE / INODE_SIZE)) * INODE_SIZE); q->i_mode = pinode->i_mode; q->i_size = pinode->i_size; q->i_start_sect = pinode->i_start_sect; @@ -841,10 +867,10 @@ static struct inode * get_inode_sched(int dev, int num) * Decrease the reference nr of a slot in inode_table[]. When the nr reaches * zero, it means the inode is not used any more and can be overwritten by * a new inode. - * + * * @param pinode I-node ptr. *****************************************************************************/ -static void put_inode(struct inode * pinode) +static void put_inode(struct inode *pinode) { // assert(pinode->i_cnt > 0); pinode->i_cnt--; @@ -856,24 +882,23 @@ static void put_inode(struct inode * pinode) /** * Write the inode back to the disk. Commonly invoked as soon as the * inode is changed. - * + * * @param p I-node ptr. *****************************************************************************/ -static void sync_inode(struct inode * p) +static void sync_inode(struct inode *p) { - struct inode * pinode; - struct super_block * sb = get_super_block(p->i_dev); + struct inode *pinode; + struct super_block *sb = get_super_block(p->i_dev); int blk_nr = 1 + 1 + sb->nr_imap_sects + sb->nr_smap_sects + ((p->i_num - 1) / (SECTOR_SIZE / INODE_SIZE)); - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - RD_SECT(p->i_dev, blk_nr, fsbuf); //modified by mingxuan 2019-5-20 - pinode = (struct inode*)((u8*)fsbuf + - (((p->i_num - 1) % (SECTOR_SIZE / INODE_SIZE)) - * INODE_SIZE)); + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + RD_SECT(p->i_dev, blk_nr, fsbuf); // modified by mingxuan 2019-5-20 + pinode = (struct inode *)((u8 *)fsbuf + + (((p->i_num - 1) % (SECTOR_SIZE / INODE_SIZE)) * INODE_SIZE)); pinode->i_mode = p->i_mode; pinode->i_size = p->i_size; pinode->i_start_sect = p->i_start_sect; pinode->i_nr_sects = p->i_nr_sects; - WR_SECT(p->i_dev, blk_nr, fsbuf); //modified by mingxuan 2019-5-20 + WR_SECT(p->i_dev, blk_nr, fsbuf); // modified by mingxuan 2019-5-20 } /// added by zcr (from ch9/e/fs/open.c) @@ -882,17 +907,17 @@ static void sync_inode(struct inode * p) *****************************************************************************/ /** * Generate a new i-node and write it to disk. - * + * * @param dev Home device of the i-node. * @param inode_nr I-node nr. * @param start_sect Start sector of the file pointed by the new i-node. - * + * * @return Ptr of the new i-node. *****************************************************************************/ -static struct inode * new_inode(int dev, int inode_nr, int start_sect) +static struct inode *new_inode(int dev, int inode_nr, int start_sect) { - //struct inode * new_inode = get_inode_sched(dev, inode_nr); //modified by xw, 18/8/28 - struct inode * new_inode = get_inode(dev, inode_nr); //modified by mingxuan 2019-5-20 + // struct inode * new_inode = get_inode_sched(dev, inode_nr); //modified by xw, 18/8/28 + struct inode *new_inode = get_inode(dev, inode_nr); // modified by mingxuan 2019-5-20 new_inode->i_mode = I_REGULAR; new_inode->i_size = 0; @@ -914,47 +939,51 @@ static struct inode * new_inode(int dev, int inode_nr, int start_sect) *****************************************************************************/ /** * Write a new entry into the directory. - * + * * @param dir_inode I-node of the directory. * @param inode_nr I-node nr of the new file. * @param filename Filename of the new file. *****************************************************************************/ -static void new_dir_entry(struct inode *dir_inode,int inode_nr,char *filename) +static void new_dir_entry(struct inode *dir_inode, int inode_nr, char *filename) { /* write the dir_entry */ int dir_blk0_nr = dir_inode->i_start_sect; int nr_dir_blks = (dir_inode->i_size + SECTOR_SIZE) / SECTOR_SIZE; int nr_dir_entries = dir_inode->i_size / DIR_ENTRY_SIZE; /** - * including unused slots - * (the file has been - * deleted but the slot - * is still there) - */ + * including unused slots + * (the file has been + * deleted but the slot + * is still there) + */ int m = 0; - struct dir_entry * pde; - struct dir_entry * new_de = 0; + struct dir_entry *pde; + struct dir_entry *new_de = 0; int i, j; - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - for (i = 0; i < nr_dir_blks; i++) { - RD_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by mingxuan 2019-5-20 + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + for (i = 0; i < nr_dir_blks; i++) + { + RD_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); // modified by mingxuan 2019-5-20 pde = (struct dir_entry *)fsbuf; - for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++,pde++) { + for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++, pde++) + { if (++m > nr_dir_entries) break; - if (pde->inode_nr == 0) { /* it's a free slot */ + if (pde->inode_nr == 0) + { /* it's a free slot */ new_de = pde; break; } } - if (m > nr_dir_entries ||/* all entries have been iterated or */ - new_de) /* free slot is found */ + if (m > nr_dir_entries || /* all entries have been iterated or */ + new_de) /* free slot is found */ break; } - if (!new_de) { /* reached the end of the dir */ + if (!new_de) + { /* reached the end of the dir */ new_de = pde; dir_inode->i_size += DIR_ENTRY_SIZE; } @@ -962,21 +991,20 @@ static void new_dir_entry(struct inode *dir_inode,int inode_nr,char *filename) strcpy(new_de->name, filename); /* write dir block -- ROOT dir block */ - WR_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //added by mingxuan 2019-5-20 + WR_SECT(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); // added by mingxuan 2019-5-20 /* update dir inode */ sync_inode(dir_inode); } - /***************************************************************************** * alloc_imap_bit *****************************************************************************/ /** * Allocate a bit in inode-map. - * + * * @param dev In which device the inode-map is located. - * + * * @return I-node nr. *****************************************************************************/ static int alloc_imap_bit(int dev) @@ -985,26 +1013,30 @@ static int alloc_imap_bit(int dev) int i, j, k; int imap_blk0_nr = 1 + 1; /* 1 boot sector & 1 super block */ - struct super_block * sb = get_super_block(dev); + struct super_block *sb = get_super_block(dev); - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 - for (i = 0; i < sb->nr_imap_sects; i++) { - RD_SECT(dev, imap_blk0_nr + i, fsbuf); //modified by mingxuan 2019-5-20 + for (i = 0; i < sb->nr_imap_sects; i++) + { + RD_SECT(dev, imap_blk0_nr + i, fsbuf); // modified by mingxuan 2019-5-20 - for (j = 0; j < SECTOR_SIZE; j++) { - if (fsbuf[j] == '\xFF') //modified by xw, 18/12/28 + for (j = 0; j < SECTOR_SIZE; j++) + { + if (fsbuf[j] == '\xFF') // modified by xw, 18/12/28 continue; /* skip `1' bits */ - for (k = 0; ((fsbuf[j] >> k) & 1) != 0; k++) {} + for (k = 0; ((fsbuf[j] >> k) & 1) != 0; k++) + { + } /* i: sector index; j: byte index; k: bit index */ inode_nr = (i * SECTOR_SIZE + j) * 8 + k; fsbuf[j] |= (1 << k); /* write the bit to imap */ - WR_SECT(dev, imap_blk0_nr + i, fsbuf); //added by mingxuan 2019-5-20 + WR_SECT(dev, imap_blk0_nr + i, fsbuf); // added by mingxuan 2019-5-20 break; } @@ -1022,10 +1054,10 @@ static int alloc_imap_bit(int dev) *****************************************************************************/ /** * Allocate a bit in sector-map. - * + * * @param dev In which device the sector-map is located. * @param nr_sects_to_alloc How many sectors are allocated. - * + * * @return The 1st sector nr allocated. *****************************************************************************/ static int alloc_smap_bit(int dev, int nr_sects_to_alloc) @@ -1036,30 +1068,37 @@ static int alloc_smap_bit(int dev, int nr_sects_to_alloc) int j; /* byte index */ int k; /* bit index */ - struct super_block * sb = get_super_block(dev); + struct super_block *sb = get_super_block(dev); int smap_blk0_nr = 1 + 1 + sb->nr_imap_sects; int free_sect_nr = 0; - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 - for (i = 0; i < sb->nr_smap_sects; i++) { /* smap_blk0_nr + i : - current sect nr. */ - //RD_SECT_SCHED(dev, smap_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 - RD_SECT(dev, smap_blk0_nr + i, fsbuf); //modified by mingxuan 2019-5-20 + for (i = 0; i < sb->nr_smap_sects; i++) + { /* smap_blk0_nr + i : +current sect nr. */ + // RD_SECT_SCHED(dev, smap_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 + RD_SECT(dev, smap_blk0_nr + i, fsbuf); // modified by mingxuan 2019-5-20 /* byte offset in current sect */ - for (j = 0; j < SECTOR_SIZE && nr_sects_to_alloc > 0; j++) { + for (j = 0; j < SECTOR_SIZE && nr_sects_to_alloc > 0; j++) + { k = 0; - if (!free_sect_nr) { + if (!free_sect_nr) + { /* loop until a free bit is found */ - //if (fsbuf[j] == 0xFF) continue; - if (fsbuf[j] == '\xFF') continue; //modified by xw, 18/12/28 - for (; ((fsbuf[j] >> k) & 1) != 0; k++) {} + // if (fsbuf[j] == 0xFF) continue; + if (fsbuf[j] == '\xFF') + continue; // modified by xw, 18/12/28 + for (; ((fsbuf[j] >> k) & 1) != 0; k++) + { + } free_sect_nr = (i * SECTOR_SIZE + j) * 8 + - k - 1 + sb->n_1st_sect; + k - 1 + sb->n_1st_sect; } - for (; k < 8; k++) { /* repeat till enough bits are set */ + for (; k < 8; k++) + { /* repeat till enough bits are set */ // assert(((fsbuf[j] >> k) & 1) == 0); fsbuf[j] |= (1 << k); if (--nr_sects_to_alloc == 0) @@ -1068,8 +1107,8 @@ static int alloc_smap_bit(int dev, int nr_sects_to_alloc) } if (free_sect_nr) /* free bit found, write the bits to smap */ - //WR_SECT_SCHED(dev, smap_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 - WR_SECT(dev, smap_blk0_nr + i, fsbuf); //added by mingxuan 2019-5-20 + // WR_SECT_SCHED(dev, smap_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 + WR_SECT(dev, smap_blk0_nr + i, fsbuf); // added by mingxuan 2019-5-20 if (nr_sects_to_alloc == 0) break; @@ -1086,17 +1125,17 @@ static int alloc_smap_bit(int dev, int nr_sects_to_alloc) *****************************************************************************/ /** * Close a file descriptor. - * + * * @param fd File descriptor. - * + * * @return Zero if successful, otherwise -1. *****************************************************************************/ -//close is a syscall interface now. added by xw, 18/6/18 -// int close(int fd) -//static int real_close(int fd) //deleted by mingxuan 2019-5-17 -int real_close(int fd) //modified by mingxuan 2019-5-17 +// close is a syscall interface now. added by xw, 18/6/18 +// int close(int fd) +// static int real_close(int fd) //deleted by mingxuan 2019-5-17 +int real_close(int fd) // modified by mingxuan 2019-5-17 { - return do_close(fd); // terrible(always returns 0) + return do_close(fd); // terrible(always returns 0) } /***************************************************************************** @@ -1104,14 +1143,14 @@ int real_close(int fd) //modified by mingxuan 2019-5-17 *****************************************************************************/ /** * Handle the message CLOSE. - * + * * @return Zero if success. *****************************************************************************/ static int do_close(int fd) { - put_inode(p_proc_current->task.filp[fd]->fd_node.fd_inode); //modified by mingxuan 2019-5-17 - p_proc_current->task.filp[fd]->fd_node.fd_inode = 0; //modified by mingxuan 2019-5-17 - p_proc_current->task.filp[fd]->flag = 0; //added by mingxuan 2019-5-17 + put_inode(p_proc_current->task.filp[fd]->fd_node.fd_inode); // modified by mingxuan 2019-5-17 + p_proc_current->task.filp[fd]->fd_node.fd_inode = 0; // modified by mingxuan 2019-5-17 + p_proc_current->task.filp[fd]->flag = 0; // added by mingxuan 2019-5-17 p_proc_current->task.filp[fd] = 0; return 0; @@ -1124,24 +1163,24 @@ static int do_close(int fd) *****************************************************************************/ /** * Read from a file descriptor. - * + * * @param fd File descriptor. * @param buf Buffer to accept the bytes read. * @param count How many bytes to read. - * + * * @return On success, the number of bytes read are returned. * On error, -1 is returned. *****************************************************************************/ -int real_read(int fd, void *buf, int count) //注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 +int real_read(int fd, void *buf, int count) // 注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 { - //added by xw, 18/8/27 + // added by xw, 18/8/27 MESSAGE fs_msg; - + fs_msg.type = READ; - fs_msg.FD = fd; - fs_msg.BUF = buf; - fs_msg.CNT = count; - fs_msg.source = proc2pid(p_proc_current); //added by xw, 18/12/22 + fs_msg.FD = fd; + fs_msg.BUF = buf; + fs_msg.CNT = count; + fs_msg.source = proc2pid(p_proc_current); // added by xw, 18/12/22 // send_recv(BOTH, TASK_FS, &msg); do_rdwt(&fs_msg); @@ -1154,33 +1193,32 @@ int real_read(int fd, void *buf, int count) //注意:buf的类型被修改成cha *****************************************************************************/ /** * Write to a file descriptor. - * + * * @param fd File descriptor. * @param buf Buffer including the bytes to write. * @param count How many bytes to write. - * + * * @return On success, the number of bytes written are returned. * On error, -1 is returned. *****************************************************************************/ -int real_write(int fd, const void *buf, int count) //注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 +int real_write(int fd, const void *buf, int count) // 注意:buf的类型被修改成char //modified by mingxuan 2019-5-17 { - //added by xw, 18/8/27 + // added by xw, 18/8/27 MESSAGE fs_msg; - + fs_msg.type = WRITE; - fs_msg.FD = fd; - fs_msg.BUF = (void*)buf; - fs_msg.CNT = count; - fs_msg.source = proc2pid(p_proc_current); //added by xw, 18/12/22 + fs_msg.FD = fd; + fs_msg.BUF = (void *)buf; + fs_msg.CNT = count; + fs_msg.source = proc2pid(p_proc_current); // added by xw, 18/12/22 // send_recv(BOTH, TASK_FS, &msg); /// zcr added do_rdwt(&fs_msg); - + return fs_msg.CNT; } - /***************************************************************************** * do_rdwt *****************************************************************************/ @@ -1189,93 +1227,102 @@ int real_write(int fd, const void *buf, int count) //注意:buf的类型被修 * * Sector map is not needed to update, since the sectors for the file have been * allocated and the bits are set when the file was created. - * + * * @return How many bytes have been read/written. *****************************************************************************/ static int do_rdwt(MESSAGE *fs_msg) { - int fd = fs_msg->FD; /**< file descriptor. */ - void * buf = fs_msg->BUF;/**< r/w buffer */ - int len = fs_msg->CNT; /**< r/w bytes */ + int fd = fs_msg->FD; /**< file descriptor. */ + void *buf = fs_msg->BUF; /**< r/w buffer */ + int len = fs_msg->CNT; /**< r/w bytes */ - int src = fs_msg->source; /* caller proc nr. */ + int src = fs_msg->source; /* caller proc nr. */ if (!(p_proc_current->task.filp[fd]->fd_mode & O_RDWR)) return -1; int pos = p_proc_current->task.filp[fd]->fd_pos; - //struct inode * pin = p_proc_current->task.filp[fd]->fd_inode; //deleted by mingxuan 2019-5-17 - struct inode * pin = p_proc_current->task.filp[fd]->fd_node.fd_inode; //modified by mingxuan 2019-5-17 + // struct inode * pin = p_proc_current->task.filp[fd]->fd_inode; //deleted by mingxuan 2019-5-17 + struct inode *pin = p_proc_current->task.filp[fd]->fd_node.fd_inode; // modified by mingxuan 2019-5-17 int imode = pin->i_mode & I_TYPE_MASK; - if (imode == I_CHAR_SPECIAL) { + if (imode == I_CHAR_SPECIAL) + { int t = fs_msg->type == READ ? DEV_READ : DEV_WRITE; fs_msg->type = t; - //added by mingxuan 2019-5-19 + // added by mingxuan 2019-5-19 int dev = pin->i_start_sect; int nr_tty = MINOR(dev); - if(MAJOR(dev) != DEV_CHAR_TTY) { + if (MAJOR(dev) != DEV_CHAR_TTY) + { panic("Error: MAJOR(dev) should be DEV_CHAR_TTY(4)\n"); } - if(fs_msg->type == DEV_READ){ - fs_msg->CNT =tty_read(&tty_table[nr_tty],buf,len); - }else if(fs_msg->type==DEV_WRITE){ - tty_write(&tty_table[nr_tty],buf,len); + if (fs_msg->type == DEV_READ) + { + fs_msg->CNT = tty_read(&tty_table[nr_tty], buf, len); + } + else if (fs_msg->type == DEV_WRITE) + { + tty_write(&tty_table[nr_tty], buf, len); } return fs_msg->CNT; } - else { + else + { int pos_end; if (fs_msg->type == READ) pos_end = min(pos + len, pin->i_size); - else /* WRITE */ + else /* WRITE */ pos_end = min(pos + len, pin->i_nr_sects * SECTOR_SIZE); int off = pos % SECTOR_SIZE; - int rw_sect_min = pin->i_start_sect + (pos>>SECTOR_SIZE_SHIFT); - int rw_sect_max = pin->i_start_sect + (pos_end>>SECTOR_SIZE_SHIFT); + int rw_sect_min = pin->i_start_sect + (pos >> SECTOR_SIZE_SHIFT); + int rw_sect_max = pin->i_start_sect + (pos_end >> SECTOR_SIZE_SHIFT); - //modified by xw, 18/12/27 + // modified by xw, 18/12/27 int chunk = min(rw_sect_max - rw_sect_min + 1, - SECTOR_SIZE >> SECTOR_SIZE_SHIFT); - + SECTOR_SIZE >> SECTOR_SIZE_SHIFT); + int bytes_rw = 0; int bytes_left = len; int i; - - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - for (i = rw_sect_min; i <= rw_sect_max; i += chunk) { + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + + for (i = rw_sect_min; i <= rw_sect_max; i += chunk) + { /* read/write this amount of bytes every time */ int bytes = min(bytes_left, chunk * SECTOR_SIZE - off); - rw_sector(DEV_READ, //modified by mingxuan 2019-5-21 - pin->i_dev, - i * SECTOR_SIZE, - chunk * SECTOR_SIZE, - proc2pid(p_proc_current), /// TASK_FS - fsbuf); - - if (fs_msg->type == READ) { - memcpy((void*)va2la(src, buf + bytes_rw), - (void*)va2la(proc2pid(p_proc_current), fsbuf + off), - bytes); - } - else { /* WRITE */ - memcpy((void*)va2la(proc2pid(p_proc_current), fsbuf + off), - (void*)va2la(src, buf + bytes_rw), - bytes); - - rw_sector(DEV_WRITE, //modified by mingxuan 2019-5-21 + rw_sector(DEV_READ, // modified by mingxuan 2019-5-21 pin->i_dev, i * SECTOR_SIZE, chunk * SECTOR_SIZE, - proc2pid(p_proc_current), + proc2pid(p_proc_current), /// TASK_FS fsbuf); + + if (fs_msg->type == READ) + { + memcpy((void *)va2la(src, buf + bytes_rw), + (void *)va2la(proc2pid(p_proc_current), fsbuf + off), + bytes); + } + else + { /* WRITE */ + memcpy((void *)va2la(proc2pid(p_proc_current), fsbuf + off), + (void *)va2la(src, buf + bytes_rw), + bytes); + + rw_sector(DEV_WRITE, // modified by mingxuan 2019-5-21 + pin->i_dev, + i * SECTOR_SIZE, + chunk * SECTOR_SIZE, + proc2pid(p_proc_current), + fsbuf); } off = 0; bytes_rw += bytes; @@ -1283,7 +1330,8 @@ static int do_rdwt(MESSAGE *fs_msg) bytes_left -= bytes; } - if (p_proc_current->task.filp[fd]->fd_pos > pin->i_size) { + if (p_proc_current->task.filp[fd]->fd_pos > pin->i_size) + { /* update inode::size */ pin->i_size = p_proc_current->task.filp[fd]->fd_pos; sync_inode(pin); @@ -1300,20 +1348,20 @@ static int do_rdwt(MESSAGE *fs_msg) *****************************************************************************/ /** * Delete a file. - * + * * @param pathname The full path of the file to delete. - * + * * @return Zero if successful, otherwise -1. *****************************************************************************/ -int real_unlink(const char * pathname) //modified by mingxuan 2019-5-17 +int real_unlink(const char *pathname) // modified by mingxuan 2019-5-17 { - //added by xw, 18/8/27 + // added by xw, 18/8/27 MESSAGE fs_msg; - - fs_msg.type = UNLINK; - fs_msg.PATHNAME = (void*)pathname; - fs_msg.NAME_LEN = strlen(pathname); - fs_msg.source = proc2pid(p_proc_current); //added by xw, 18/12/22 + + fs_msg.type = UNLINK; + fs_msg.PATHNAME = (void *)pathname; + fs_msg.NAME_LEN = strlen(pathname); + fs_msg.source = proc2pid(p_proc_current); // added by xw, 18/12/22 // send_recv(BOTH, TASK_FS, &msg); @@ -1331,7 +1379,7 @@ int real_unlink(const char * pathname) //modified by mingxuan 2019-5-17 * * @note We clear the i-node in inode_array[] although it is not really needed. * We don't clear the data bytes so the file is recoverable. - * + * * @return On success, zero is returned. On error, -1 is returned. *****************************************************************************/ static int do_unlink(MESSAGE *fs_msg) @@ -1339,43 +1387,47 @@ static int do_unlink(MESSAGE *fs_msg) char pathname[MAX_PATH]; /* get parameters from the message */ - int name_len = fs_msg->NAME_LEN; /* length of filename */ - int src = fs_msg->source; /* caller proc nr. */ + int name_len = fs_msg->NAME_LEN; /* length of filename */ + int src = fs_msg->source; /* caller proc nr. */ // assert(name_len < MAX_PATH); - memcpy((void*)va2la(proc2pid(p_proc_current), pathname), - (void*)va2la(src, fs_msg->PATHNAME), - name_len); + memcpy((void *)va2la(proc2pid(p_proc_current), pathname), + (void *)va2la(src, fs_msg->PATHNAME), + name_len); pathname[name_len] = 0; - if (strcmp(pathname , "/") == 0) { + if (strcmp(pathname, "/") == 0) + { kprintf("FS:do_unlink():: cannot unlink the root\n"); return -1; } int inode_nr = search_file(pathname); - if (inode_nr == INVALID_INODE) { /* file not found */ + if (inode_nr == INVALID_INODE) + { /* file not found */ kprintf("FS::do_unlink():: search_file() returns invalid inode: %s\n", pathname); return -1; } char filename[MAX_PATH]; - struct inode * dir_inode; + struct inode *dir_inode; if (strip_path(filename, pathname, &dir_inode) != 0) return -1; - struct inode * pin = get_inode_sched(dir_inode->i_dev, inode_nr); //modified by xw, 18/8/28 + struct inode *pin = get_inode_sched(dir_inode->i_dev, inode_nr); // modified by xw, 18/8/28 - if (pin->i_mode != I_REGULAR) { /* can only remove regular files */ + if (pin->i_mode != I_REGULAR) + { /* can only remove regular files */ kprintf("cannot remove file %s, because it is not a regular file.\n", pathname); return -1; } - if (pin->i_cnt > 1) { /* the file was opened */ + if (pin->i_cnt > 1) + { /* the file was opened */ kprintf("cannot remove file %s, because pin->i_cnt is %d\n", pathname, pin->i_cnt); return -1; } - struct super_block * sb = get_super_block(pin->i_dev); + struct super_block *sb = get_super_block(pin->i_dev); /*************************/ /* free the bit in i-map */ @@ -1384,11 +1436,11 @@ static int do_unlink(MESSAGE *fs_msg) int bit_idx = inode_nr % 8; // assert(byte_idx < SECTOR_SIZE); /* we have only one i-map sector */ /* read sector 2 (skip bootsect and superblk): */ - char fsbuf[SECTOR_SIZE]; //local array, to substitute global fsbuf. added by xw, 18/12/27 - RD_SECT_SCHED(pin->i_dev, 2, fsbuf); //modified by xw, 18/12/27 + char fsbuf[SECTOR_SIZE]; // local array, to substitute global fsbuf. added by xw, 18/12/27 + RD_SECT_SCHED(pin->i_dev, 2, fsbuf); // modified by xw, 18/12/27 // assert(fsbuf[byte_idx % SECTOR_SIZE] & (1 << bit_idx)); fsbuf[byte_idx % SECTOR_SIZE] &= ~(1 << bit_idx); - WR_SECT_SCHED(pin->i_dev, 2, fsbuf); //modified by xw, 18/12/27 + WR_SECT_SCHED(pin->i_dev, 2, fsbuf); // modified by xw, 18/12/27 /**************************/ /* free the bits in s-map */ @@ -1405,46 +1457,50 @@ static int do_unlink(MESSAGE *fs_msg) * ...__/ * byte_idx: byte idx in the entire i-map */ - bit_idx = pin->i_start_sect - sb->n_1st_sect + 1; + bit_idx = pin->i_start_sect - sb->n_1st_sect + 1; byte_idx = bit_idx / 8; int bits_left = pin->i_nr_sects; int byte_cnt = (bits_left - (8 - (bit_idx % 8))) / 8; /* current sector nr. */ - int s = 2 /* 2: bootsect + superblk */ - + sb->nr_imap_sects + byte_idx / SECTOR_SIZE; + int s = 2 /* 2: bootsect + superblk */ + + sb->nr_imap_sects + byte_idx / SECTOR_SIZE; - RD_SECT_SCHED(pin->i_dev, s, fsbuf); //modified by xw, 18/12/27 + RD_SECT_SCHED(pin->i_dev, s, fsbuf); // modified by xw, 18/12/27 int i; /* clear the first byte */ - for (i = bit_idx % 8; (i < 8) && bits_left; i++,bits_left--) { + for (i = bit_idx % 8; (i < 8) && bits_left; i++, bits_left--) + { // assert((fsbuf[byte_idx % SECTOR_SIZE] >> i & 1) == 1); fsbuf[byte_idx % SECTOR_SIZE] &= ~(1 << i); } /* clear bytes from the second byte to the second to last */ int k; - i = (byte_idx % SECTOR_SIZE) + 1; /* the second byte */ - for (k = 0; k < byte_cnt; k++,i++,bits_left-=8) { - if (i == SECTOR_SIZE) { + i = (byte_idx % SECTOR_SIZE) + 1; /* the second byte */ + for (k = 0; k < byte_cnt; k++, i++, bits_left -= 8) + { + if (i == SECTOR_SIZE) + { i = 0; - WR_SECT_SCHED(pin->i_dev, s, fsbuf); //modified by xw, 18/12/27 - RD_SECT_SCHED(pin->i_dev, ++s, fsbuf); //modified by xw, 18/12/27 + WR_SECT_SCHED(pin->i_dev, s, fsbuf); // modified by xw, 18/12/27 + RD_SECT_SCHED(pin->i_dev, ++s, fsbuf); // modified by xw, 18/12/27 } // assert(fsbuf[i] == 0xFF); fsbuf[i] = 0; } /* clear the last byte */ - if (i == SECTOR_SIZE) { + if (i == SECTOR_SIZE) + { i = 0; - WR_SECT_SCHED(pin->i_dev, s, fsbuf); //modified by xw, 18/12/27 - RD_SECT_SCHED(pin->i_dev, ++s, fsbuf); //modified by xw, 18/12/27 + WR_SECT_SCHED(pin->i_dev, s, fsbuf); // modified by xw, 18/12/27 + RD_SECT_SCHED(pin->i_dev, ++s, fsbuf); // modified by xw, 18/12/27 } // assert((fsbuf[i] & mask) == mask); fsbuf[i] &= (~0) << bits_left; - WR_SECT_SCHED(pin->i_dev, s, fsbuf); //modified by xw, 18/12/27 + WR_SECT_SCHED(pin->i_dev, s, fsbuf); // modified by xw, 18/12/27 /***************************/ /* clear the i-node itself */ @@ -1464,28 +1520,31 @@ static int do_unlink(MESSAGE *fs_msg) int nr_dir_blks = (dir_inode->i_size + SECTOR_SIZE) / SECTOR_SIZE; int nr_dir_entries = dir_inode->i_size / DIR_ENTRY_SIZE; /* including unused slots - * (the file has been - * deleted but the slot - * is still there) - */ + * (the file has been + * deleted but the slot + * is still there) + */ int m = 0; - struct dir_entry * pde = 0; + struct dir_entry *pde = 0; int flg = 0; int dir_size = 0; - for (i = 0; i < nr_dir_blks; i++) { - RD_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 + for (i = 0; i < nr_dir_blks; i++) + { + RD_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); // modified by xw, 18/12/27 pde = (struct dir_entry *)fsbuf; int j; - for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++,pde++) { + for (j = 0; j < SECTOR_SIZE / DIR_ENTRY_SIZE; j++, pde++) + { if (++m > nr_dir_entries) break; - if (pde->inode_nr == inode_nr) { + if (pde->inode_nr == inode_nr) + { /* pde->inode_nr = 0; */ memset(pde, 0, DIR_ENTRY_SIZE); - WR_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); //modified by xw, 18/12/27 + WR_SECT_SCHED(dir_inode->i_dev, dir_blk0_nr + i, fsbuf); // modified by xw, 18/12/27 flg = 1; break; } @@ -1495,11 +1554,12 @@ static int do_unlink(MESSAGE *fs_msg) } if (m > nr_dir_entries || /* all entries have been iterated OR */ - flg) /* file is found */ + flg) /* file is found */ break; } // assert(flg); - if (m == nr_dir_entries) { /* the file is the last one in the dir */ + if (m == nr_dir_entries) + { /* the file is the last one in the dir */ dir_inode->i_size = dir_size; sync_inode(dir_inode); } @@ -1507,11 +1567,11 @@ static int do_unlink(MESSAGE *fs_msg) return 0; } -int real_lseek(int fd, int offset, int whence) //modified by mingxuan 2019-5-17 +int real_lseek(int fd, int offset, int whence) // modified by mingxuan 2019-5-17 { - //added by xw, 18/8/27 + // added by xw, 18/8/27 MESSAGE fs_msg; - + fs_msg.FD = fd; fs_msg.OFFSET = offset; fs_msg.WHENCE = whence; @@ -1524,7 +1584,7 @@ int real_lseek(int fd, int offset, int whence) //modified by mingxuan 2019-5-17 *****************************************************************************/ /** * Handle the message LSEEK. - * + * * @return The new offset in bytes from the beginning of the file if successful, * otherwise a negative number. *****************************************************************************/ @@ -1535,10 +1595,11 @@ static int do_lseek(MESSAGE *fs_msg) int whence = fs_msg->WHENCE; int pos = p_proc_current->task.filp[fd]->fd_pos; - //int f_size = p_proc_current->task.filp[fd]->fd_inode->i_size; //deleted by mingxuan 2019-5-17 - int f_size = p_proc_current->task.filp[fd]->fd_node.fd_inode->i_size; //modified by mingxuan 2019-5-17 + // int f_size = p_proc_current->task.filp[fd]->fd_inode->i_size; //deleted by mingxuan 2019-5-17 + int f_size = p_proc_current->task.filp[fd]->fd_node.fd_inode->i_size; // modified by mingxuan 2019-5-17 - switch (whence) { + switch (whence) + { case SEEK_SET: pos = off; break; @@ -1552,7 +1613,8 @@ static int do_lseek(MESSAGE *fs_msg) return -1; break; } - if ((pos > f_size) || (pos < 0)) { + if ((pos > f_size) || (pos < 0)) + { return -1; } p_proc_current->task.filp[fd]->fd_pos = pos; diff --git a/kernel/main.c b/kernel/main.c index a98142f..09ce2c0 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,8 +1,8 @@ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - main.c + main.c ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - Forrest Yu, 2005 + Forrest Yu, 2005 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ #include "type.h" @@ -23,11 +23,11 @@ #include "stdio.h" #include "tty.h" -static int initialize_processes(); //added by xw, 18/5/26 -static int initialize_cpus(); //added by xw, 18/6/2 +static int initialize_processes(); // added by xw, 18/5/26 +static int initialize_cpus(); // added by xw, 18/6/2 /*======================================================================* - kernel_main + kernel_main *======================================================================*/ int kernel_main() { @@ -43,83 +43,85 @@ int kernel_main() disp_pos = 0; kprintf("-----Kernel Initialization Begins-----\n"); - kernel_initial = 1; //kernel is in initial state. added by xw, 18/5/31 - - init();//内存管理模块的初始化 add by liang + kernel_initial = 1; // kernel is in initial state. added by xw, 18/5/31 + + init(); // 内存管理模块的初始化 add by liang init_tty_main(); - //initialize PCBs, added by xw, 18/5/26 + // initialize PCBs, added by xw, 18/5/26 error = initialize_processes(); - if(error != 0) + if (error != 0) return error; - //initialize CPUs, added by xw, 18/6/2 + // initialize CPUs, added by xw, 18/6/2 error = initialize_cpus(); - if(error != 0) + if (error != 0) return error; - - k_reenter = 0; //record nest level of only interruption! it's different from Orange's. - //usage modified by xw - ticks = 0; //initialize system-wide ticks + + k_reenter = 0; // record nest level of only interruption! it's different from Orange's. + // usage modified by xw + ticks = 0; // initialize system-wide ticks p_proc_current = cpu_table; /************************************************************************ *device initialization added by xw, 18/6/4 *************************************************************************/ - /* initialize 8253 PIT */ - outb(TIMER_MODE, RATE_GENERATOR); - outb(TIMER0, (u8) (TIMER_FREQ/HZ) ); - outb(TIMER0, (u8) ((TIMER_FREQ/HZ) >> 8)); - + /* initialize 8253 PIT */ + outb(TIMER_MODE, RATE_GENERATOR); + outb(TIMER0, (u8)(TIMER_FREQ / HZ)); + outb(TIMER0, (u8)((TIMER_FREQ / HZ) >> 8)); + /* initialize clock-irq */ - put_irq_handler(CLOCK_IRQ, clock_handler); /* 设定时钟中断处理程序 */ - enable_irq(CLOCK_IRQ); /* 让8259A可以接收时钟中断 */ - - init_kb(); //added by mingxuan 2019-5-19 - + put_irq_handler(CLOCK_IRQ, clock_handler); /* 设定时钟中断处理程序 */ + enable_irq(CLOCK_IRQ); /* 让8259A可以接收时钟中断 */ + + init_kb(); // added by mingxuan 2019-5-19 + /* initialize hd-irq and hd rdwt queue */ init_hd(); - + /* enable interrupt, we should read information of some devices by interrupt. * Note that you must have initialized all devices ready before you enable * interrupt. added by xw */ enable_int(); - - /*********************************************************************** + + /*********************************************************************** open hard disk and initialize file system coded by zcr on 2017.6.10. added by xw, 18/5/31 ************************************************************************/ - init_fileop_table(); //added by mingxuan 2019-5-17 + init_fileop_table(); // added by mingxuan 2019-5-17 - //hd_open(MINOR(ROOT_DEV)); - hd_open(PRIMARY_MASTER); //modified by mingxuan 2020-10-27 + // hd_open(MINOR(ROOT_DEV)); + hd_open(PRIMARY_MASTER); // modified by mingxuan 2020-10-27 - init_vfs(); //added by mingxuan 2020-10-30 + init_vfs(); // added by mingxuan 2020-10-30 init_fs(); - init_fs_fat(); //added by mingxuan 2019-5-17 - //init_vfs(); //added by mingxuan 2019-5-17 //deleted by mingxuan 2020-10-30 + init_fs_fat(); // added by mingxuan 2019-5-17 + // init_vfs(); //added by mingxuan 2019-5-17 //deleted by mingxuan 2020-10-30 /************************************************************************* - *第一个进程开始启动执行 - **************************************************************************/ + *第一个进程开始启动执行 + **************************************************************************/ /* we don't want interrupt happens before processes run. * added by xw, 18/5/31 */ disable_int(); - + kprintf("-----Processes Begin-----\n"); - + /* linear address 0~8M will no longer be mapped to physical address 0~8M. * note that disp_xx can't work after this function is invoked until processes runs. * add by visual 2016.5.13; moved by xw, 18/5/30 */ clear_kernel_pagepte_low(); - + p_proc_current = proc_table; - kernel_initial = 0; //kernel initialization is done. added by xw, 18/5/31 - restart_initial(); //modified by xw, 18/4/19 - while(1){} + kernel_initial = 0; // kernel initialization is done. added by xw, 18/5/31 + restart_initial(); // modified by xw, 18/4/19 + while (1) + { + } } /************************************************************************* @@ -128,9 +130,9 @@ added by xw, 18/6/2 ***************************************************************************/ static int initialize_cpus() { - //just use the fields of struct PCB in cpu_table, we needn't initialize - //something at present. - + // just use the fields of struct PCB in cpu_table, we needn't initialize + // something at present. + return 0; } @@ -141,334 +143,329 @@ moved from kernel_main() by xw, 18/5/26 ***************************************************************************/ static int initialize_processes() { - TASK* p_task = task_table; - PROCESS* p_proc = proc_table; - u16 selector_ldt = SELECTOR_LDT_FIRST; - char* p_regs; //point to registers in the new kernel stack, added by xw, 17/12/11 - task_f eip_context; //a funtion pointer, added by xw, 18/4/18 + TASK *p_task = task_table; + PROCESS *p_proc = proc_table; + u16 selector_ldt = SELECTOR_LDT_FIRST; + char *p_regs; // point to registers in the new kernel stack, added by xw, 17/12/11 + task_f eip_context; // a funtion pointer, added by xw, 18/4/18 /************************************************************************* - *进程初始化部分 edit by visual 2016.5.4 - ***************************************************************************/ + *进程初始化部分 edit by visual 2016.5.4 + ***************************************************************************/ int pid; - u32 AddrLin,err_temp;//edit by visual 2016.5.9 - + u32 AddrLin, err_temp; // edit by visual 2016.5.9 + /* set common fields in PCB. added by xw, 18/5/25 */ p_proc = proc_table; - for( pid=0 ; pid对前NR_TASKS个PCB初始化,且状态为READY(生成的进程) + + p_proc = proc_table; + for (pid = 0; pid < NR_TASKS; pid++) + { // 1>对前NR_TASKS个PCB初始化,且状态为READY(生成的进程) /*************基本信息*********************************/ - strcpy(p_proc->task.p_name, p_task->name); //名称 - p_proc->task.pid = pid; //pid - p_proc->task.stat = READY; //状态 - + strcpy(p_proc->task.p_name, p_task->name); // 名称 + p_proc->task.pid = pid; // pid + p_proc->task.stat = READY; // 状态 + /**************LDT*********************************/ p_proc->task.ldt_sel = selector_ldt; - memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[0].attr1 = DA_C | PRIVILEGE_TASK << 5; - memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[1].attr1 = DA_DRW | PRIVILEGE_TASK << 5; - + /**************寄存器初值**********************************/ - p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK)| RPL_TASK; + p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK) | RPL_TASK; p_proc->task.regs.eflags = 0x1202; /* IF=1, IOPL=1 */ - //p_proc->task.cr3 在页表初始化中处理 - - - /**************线性地址布局初始化**********************************/// add by visual 2016.5.4 + // p_proc->task.cr3 在页表初始化中处理 + + /**************线性地址布局初始化**********************************/ // add by visual 2016.5.4 /**************task的代码数据大小及位置暂时是不会用到的,所以没有初始化************************************/ p_proc->task.memmap.heap_lin_base = HeapLinBase; - p_proc->task.memmap.heap_lin_limit = HeapLinBase; //堆的界限将会一直动态变化 + p_proc->task.memmap.heap_lin_limit = HeapLinBase; // 堆的界限将会一直动态变化 - p_proc->task.memmap.stack_child_limit = StackLinLimitMAX; //add by visual 2016.5.27 + p_proc->task.memmap.stack_child_limit = StackLinLimitMAX; // add by visual 2016.5.27 p_proc->task.memmap.stack_lin_base = StackLinBase; - p_proc->task.memmap.stack_lin_limit = StackLinBase - 0x4000; //栈的界限将会一直动态变化,目前赋值为16k,这个值会根据esp的位置进行调整,目前初始化为16K大小 - + p_proc->task.memmap.stack_lin_limit = StackLinBase - 0x4000; // 栈的界限将会一直动态变化,目前赋值为16k,这个值会根据esp的位置进行调整,目前初始化为16K大小 + p_proc->task.memmap.kernel_lin_base = KernelLinBase; - p_proc->task.memmap.kernel_lin_limit = KernelLinBase + KernelSize; //内核大小初始化为8M //add by visual 2016.5.10 - + p_proc->task.memmap.kernel_lin_limit = KernelLinBase + KernelSize; // 内核大小初始化为8M //add by visual 2016.5.10 + /***************初始化PID进程页表*****************************/ - if( 0 != init_page_pte(pid) ) + if (0 != init_page_pte(pid)) { - disp_color_str("kernel_main Error:init_page_pte",0x74); + disp_color_str("kernel_main Error:init_page_pte", 0x74); return -1; } // pde_addr_phy_temp = get_pde_phy_addr(pid);//获取该进程页目录物理地址 //delete by visual 2016.5.19 - + /****************代码数据*****************************/ - p_proc->task.regs.eip= (u32)p_task->initial_eip;//进程入口线性地址 edit by visual 2016.5.4 - + p_proc->task.regs.eip = (u32)p_task->initial_eip; // 进程入口线性地址 edit by visual 2016.5.4 + /****************栈(此时堆、栈已经区分,以后实验会重新规划堆的位置)*****************************/ - p_proc->task.regs.esp=(u32)StackLinBase; //栈地址最高处 - for( AddrLin=StackLinBase ; AddrLin>p_proc->task.memmap.stack_lin_limit ; AddrLin-=num_4K ) - {//栈 - //addr_phy_temp = (u32)do_kmalloc_4k();//为栈申请一个物理页,Task的栈是在内核里面 //delete by visual 2016.5.19 - //if( addr_phy_temp<0 || (addr_phy_temp&0x3FF)!=0 ) + p_proc->task.regs.esp = (u32)StackLinBase; // 栈地址最高处 + for (AddrLin = StackLinBase; AddrLin > p_proc->task.memmap.stack_lin_limit; AddrLin -= num_4K) + { // 栈 + // 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); // return -1; - //} - err_temp = lin_mapping_phy( AddrLin,//线性地址 //add by visual 2016.5.9 - MAX_UNSIGNED_INT,//物理地址 //edit by visual 2016.5.19 - pid,//进程pid //edit by visual 2016.5.19 - PG_P | PG_USU | PG_RWW,//页目录的属性位 - PG_P | PG_USU | PG_RWW);//页表的属性位 - if( err_temp!=0 ) + // } + err_temp = lin_mapping_phy(AddrLin, // 线性地址 //add by visual 2016.5.9 + MAX_UNSIGNED_INT, // 物理地址 //edit by visual 2016.5.19 + pid, // 进程pid //edit by visual 2016.5.19 + PG_P | PG_USU | PG_RWW, // 页目录的属性位 + PG_P | PG_USU | PG_RWW); // 页表的属性位 + if (err_temp != 0) { - disp_color_str("kernel_main Error:lin_mapping_phy",0x74); + disp_color_str("kernel_main Error:lin_mapping_phy", 0x74); return -1; } - } - + /***************copy registers data to kernel stack****************************/ - //copy registers data to the bottom of the new kernel stack - //added by xw, 17/12/11 - p_regs = (char*)(p_proc + 1); + // copy registers data to the bottom of the new kernel stack + // added by xw, 17/12/11 + p_regs = (char *)(p_proc + 1); p_regs -= P_STACKTOP; - memcpy(p_regs, (char*)p_proc, 18 * 4); - + memcpy(p_regs, (char *)p_proc, 18 * 4); + /***************some field about process switch****************************/ - p_proc->task.esp_save_int = p_regs; //initialize esp_save_int, added by xw, 17/12/11 - //p_proc->task.save_type = 1; - p_proc->task.esp_save_context = p_regs - 10 * 4; //when the process is chosen to run for the first time, - //sched() will fetch value from esp_save_context + p_proc->task.esp_save_int = p_regs; // initialize esp_save_int, added by xw, 17/12/11 + // p_proc->task.save_type = 1; + p_proc->task.esp_save_context = p_regs - 10 * 4; // when the process is chosen to run for the first time, + // sched() will fetch value from esp_save_context eip_context = restart_restore; - *(u32*)(p_regs - 4) = (u32)eip_context; //initialize EIP in the context, so the process can - //start run. added by xw, 18/4/18 - *(u32*)(p_regs - 8) = 0x1202; //initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 - + *(u32 *)(p_regs - 4) = (u32)eip_context; // initialize EIP in the context, so the process can + // start run. added by xw, 18/4/18 + *(u32 *)(p_regs - 8) = 0x1202; // initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 + /***************变量调整****************************/ p_proc++; p_task++; selector_ldt += 1 << 3; } - for( ; pid对中NR_TASKS~NR_K_PCBS的PCB表初始化,状态为IDLE,没有初始化esp(并没有生成,所以没有代码入口,只是留位置) + for (; pid < NR_K_PCBS; pid++) + { // 2>对中NR_TASKS~NR_K_PCBS的PCB表初始化,状态为IDLE,没有初始化esp(并没有生成,所以没有代码入口,只是留位置) /*************基本信息*********************************/ - strcpy(p_proc->task.p_name, "Task"); //名称 - p_proc->task.pid = pid; //pid - p_proc->task.stat = IDLE; //状态 - + strcpy(p_proc->task.p_name, "Task"); // 名称 + p_proc->task.pid = pid; // pid + p_proc->task.stat = IDLE; // 状态 + /**************LDT*********************************/ p_proc->task.ldt_sel = selector_ldt; - memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[0].attr1 = DA_C | PRIVILEGE_TASK << 5; - memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[1].attr1 = DA_DRW | PRIVILEGE_TASK << 5; - + /**************寄存器初值**********************************/ - p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK)| RPL_TASK; - p_proc->task.regs.eflags = 0x1202; /* IF=1, IOPL=1 */ - + p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK) | RPL_TASK; + p_proc->task.regs.eflags = 0x1202; /* IF=1, IOPL=1 */ + /****************页表、代码数据、堆栈*****************************/ - //无 - + // 无 + /***************copy registers data to kernel stack****************************/ - //copy registers data to the bottom of the new kernel stack - //added by xw, 17/12/11 - p_regs = (char*)(p_proc + 1); + // copy registers data to the bottom of the new kernel stack + // added by xw, 17/12/11 + p_regs = (char *)(p_proc + 1); p_regs -= P_STACKTOP; - memcpy(p_regs, (char*)p_proc, 18 * 4); - + memcpy(p_regs, (char *)p_proc, 18 * 4); + /***************some field about process switch****************************/ - p_proc->task.esp_save_int = p_regs; //initialize esp_save_int, added by xw, 17/12/11 - //p_proc->task.save_type = 1; - p_proc->task.esp_save_context = p_regs - 10 * 4; //when the process is chosen to run for the first time, - //sched() will fetch value from esp_save_context + p_proc->task.esp_save_int = p_regs; // initialize esp_save_int, added by xw, 17/12/11 + // p_proc->task.save_type = 1; + p_proc->task.esp_save_context = p_regs - 10 * 4; // when the process is chosen to run for the first time, + // sched() will fetch value from esp_save_context eip_context = restart_restore; - *(u32*)(p_regs - 4) = (u32)eip_context; //initialize EIP in the context, so the process can - //start run. added by xw, 18/4/18 - *(u32*)(p_regs - 8) = 0x1202; //initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 + *(u32 *)(p_regs - 4) = (u32)eip_context; // initialize EIP in the context, so the process can + // start run. added by xw, 18/4/18 + *(u32 *)(p_regs - 8) = 0x1202; // initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 /***************变量调整****************************/ p_proc++; selector_ldt += 1 << 3; } - for( ; pidtask.p_name,"initial"); //名称 - p_proc->task.pid = pid; //pid - p_proc->task.stat = READY; //状态 - + strcpy(p_proc->task.p_name, "initial"); // 名称 + p_proc->task.pid = pid; // pid + p_proc->task.stat = READY; // 状态 + /**************LDT*********************************/ p_proc->task.ldt_sel = selector_ldt; - memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[0].attr1 = DA_C | PRIVILEGE_TASK << 5; - memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[1].attr1 = DA_DRW | PRIVILEGE_TASK << 5; - + /**************寄存器初值**********************************/ - p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_TASK; - p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK)| RPL_TASK; + p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK; + p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK) | RPL_TASK; p_proc->task.regs.eflags = 0x1202; /* IF=1, IOPL=1 */ - //p_proc->task.cr3 在页表初始化中处理 - - - /**************线性地址布局初始化**********************************/ //edit by visual 2016.5.25 - p_proc->task.memmap.text_lin_base = 0; //initial这些段的数据并不清楚,在变身init的时候才在中赋新值 - p_proc->task.memmap.text_lin_limit = 0; //initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 - p_proc->task.memmap.data_lin_base = 0; //initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 - p_proc->task.memmap.data_lin_limit= 0; //initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 - p_proc->task.memmap.vpage_lin_base = VpageLinBase; //保留内存基址 - p_proc->task.memmap.vpage_lin_limit = VpageLinBase; //保留内存界限 - p_proc->task.memmap.heap_lin_base = HeapLinBase; //堆基址 - p_proc->task.memmap.heap_lin_limit = HeapLinBase; //堆界限 - p_proc->task.memmap.stack_lin_base = StackLinBase; //栈基址 - p_proc->task.memmap.stack_lin_limit = StackLinBase - 0x4000; //栈界限(使用时注意栈的生长方向) - p_proc->task.memmap.arg_lin_base = ArgLinBase; //参数内存基址 - p_proc->task.memmap.arg_lin_limit = ArgLinBase; //参数内存界限 - p_proc->task.memmap.kernel_lin_base = KernelLinBase; //内核基址 - p_proc->task.memmap.kernel_lin_limit = KernelLinBase + KernelSize; //内核大小初始化为8M - + // p_proc->task.cr3 在页表初始化中处理 + + /**************线性地址布局初始化**********************************/ // edit by visual 2016.5.25 + p_proc->task.memmap.text_lin_base = 0; // initial这些段的数据并不清楚,在变身init的时候才在中赋新值 + p_proc->task.memmap.text_lin_limit = 0; // initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 + p_proc->task.memmap.data_lin_base = 0; // initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 + p_proc->task.memmap.data_lin_limit = 0; // initial这些段的数据并不清楚,在变身init的时候才在exec中赋新值 + p_proc->task.memmap.vpage_lin_base = VpageLinBase; // 保留内存基址 + p_proc->task.memmap.vpage_lin_limit = VpageLinBase; // 保留内存界限 + p_proc->task.memmap.heap_lin_base = HeapLinBase; // 堆基址 + p_proc->task.memmap.heap_lin_limit = HeapLinBase; // 堆界限 + p_proc->task.memmap.stack_lin_base = StackLinBase; // 栈基址 + p_proc->task.memmap.stack_lin_limit = StackLinBase - 0x4000; // 栈界限(使用时注意栈的生长方向) + p_proc->task.memmap.arg_lin_base = ArgLinBase; // 参数内存基址 + p_proc->task.memmap.arg_lin_limit = ArgLinBase; // 参数内存界限 + p_proc->task.memmap.kernel_lin_base = KernelLinBase; // 内核基址 + p_proc->task.memmap.kernel_lin_limit = KernelLinBase + KernelSize; // 内核大小初始化为8M + /*************************进程树信息初始化***************************************/ - p_proc->task.info.type = TYPE_PROCESS; //当前是进程还是线程 - p_proc->task.info.real_ppid = -1; //亲父进程,创建它的那个进程 - p_proc->task.info.ppid = -1; //当前父进程 - p_proc->task.info.child_p_num = 0; //子进程数量 - //p_proc->task.info.child_process[NR_CHILD_MAX];//子进程列表 - p_proc->task.info.child_t_num = 0; //子线程数量 - //p_proc->task.info.child_thread[NR_CHILD_MAX];//子线程列表 - p_proc->task.info.text_hold = 1; //是否拥有代码 - p_proc->task.info.data_hold = 1; //是否拥有数据 - - + p_proc->task.info.type = TYPE_PROCESS; // 当前是进程还是线程 + p_proc->task.info.real_ppid = -1; // 亲父进程,创建它的那个进程 + p_proc->task.info.ppid = -1; // 当前父进程 + p_proc->task.info.child_p_num = 0; // 子进程数量 + // p_proc->task.info.child_process[NR_CHILD_MAX];//子进程列表 + p_proc->task.info.child_t_num = 0; // 子线程数量 + // p_proc->task.info.child_thread[NR_CHILD_MAX];//子线程列表 + p_proc->task.info.text_hold = 1; // 是否拥有代码 + p_proc->task.info.data_hold = 1; // 是否拥有数据 + /***************初始化PID进程页表*****************************/ - if( 0 != init_page_pte(pid) ) + if (0 != init_page_pte(pid)) { - disp_color_str("kernel_main Error:init_page_pte",0x74); + disp_color_str("kernel_main Error:init_page_pte", 0x74); return -1; } - //pde_addr_phy_temp = get_pde_phy_addr(pid);//获取该进程页目录物理地址 //edit by visual 2016.5.19 - + // pde_addr_phy_temp = get_pde_phy_addr(pid);//获取该进程页目录物理地址 //edit by visual 2016.5.19 + /****************代码数据*****************************/ - p_proc->task.regs.eip= (u32)initial;//进程入口线性地址 edit by visual 2016.5.17 - + p_proc->task.regs.eip = (u32)initial; // 进程入口线性地址 edit by visual 2016.5.17 + /****************栈(此时堆、栈已经区分,以后实验会重新规划堆的位置)*****************************/ - p_proc->task.regs.esp=(u32)StackLinBase; //栈地址最高处 - for( AddrLin=StackLinBase ; AddrLin>p_proc->task.memmap.stack_lin_limit ; AddrLin-=num_4K ) - {//栈 - //addr_phy_temp = (u32)do_kmalloc_4k();//为栈申请一个物理页,Task的栈是在内核里面 //delete by visual 2016.5.19 - //if( addr_phy_temp<0 || (addr_phy_temp&0x3FF)!=0 ) + p_proc->task.regs.esp = (u32)StackLinBase; // 栈地址最高处 + for (AddrLin = StackLinBase; AddrLin > p_proc->task.memmap.stack_lin_limit; AddrLin -= num_4K) + { // 栈 + // 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); // return -1; - //} - err_temp = lin_mapping_phy( AddrLin,//线性地址 - MAX_UNSIGNED_INT,//物理地址 //edit by visual 2016.5.19 - pid,//进程pid //edit by visual 2016.5.19 - PG_P | PG_USU | PG_RWW,//页目录的属性位 - PG_P | PG_USU | PG_RWW);//页表的属性位 - if( err_temp!=0 ) + // } + err_temp = lin_mapping_phy(AddrLin, // 线性地址 + MAX_UNSIGNED_INT, // 物理地址 //edit by visual 2016.5.19 + pid, // 进程pid //edit by visual 2016.5.19 + PG_P | PG_USU | PG_RWW, // 页目录的属性位 + PG_P | PG_USU | PG_RWW); // 页表的属性位 + if (err_temp != 0) { - disp_color_str("kernel_main Error:lin_mapping_phy",0x74); + disp_color_str("kernel_main Error:lin_mapping_phy", 0x74); return -1; } - } - + /***************copy registers data to kernel stack****************************/ - //copy registers data to the bottom of the new kernel stack - //added by xw, 17/12/11 - p_regs = (char*)(p_proc + 1); + // copy registers data to the bottom of the new kernel stack + // added by xw, 17/12/11 + p_regs = (char *)(p_proc + 1); p_regs -= P_STACKTOP; - memcpy(p_regs, (char*)p_proc, 18 * 4); - + memcpy(p_regs, (char *)p_proc, 18 * 4); + /***************some field about process switch****************************/ - p_proc->task.esp_save_int = p_regs; //initialize esp_save_int, added by xw, 17/12/11 - //p_proc->task.save_type = 1; - p_proc->task.esp_save_context = p_regs - 10 * 4; //when the process is chosen to run for the first time, - //sched() will fetch value from esp_save_context + p_proc->task.esp_save_int = p_regs; // initialize esp_save_int, added by xw, 17/12/11 + // p_proc->task.save_type = 1; + p_proc->task.esp_save_context = p_regs - 10 * 4; // when the process is chosen to run for the first time, + // sched() will fetch value from esp_save_context eip_context = restart_restore; - *(u32*)(p_regs - 4) = (u32)eip_context; //initialize EIP in the context, so the process can - //start run. added by xw, 18/4/18 - *(u32*)(p_regs - 8) = 0x1202; //initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 - + *(u32 *)(p_regs - 4) = (u32)eip_context; // initialize EIP in the context, so the process can + // start run. added by xw, 18/4/18 + *(u32 *)(p_regs - 8) = 0x1202; // initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 + /***************变量调整****************************/ p_proc++; selector_ldt += 1 << 3; } - for( ; pid对后NR_K_PCBS~NR_PCBS的PCB表部分初始化,(名称,pid,stat,LDT选择子),状态为IDLE. + for (; pid < NR_PCBS; pid++) + { // 3>对后NR_K_PCBS~NR_PCBS的PCB表部分初始化,(名称,pid,stat,LDT选择子),状态为IDLE. /*************基本信息*********************************/ - strcpy(p_proc->task.p_name, "USER"); //名称 - p_proc->task.pid = pid; //pid - p_proc->task.stat = IDLE; //状态 - + strcpy(p_proc->task.p_name, "USER"); // 名称 + p_proc->task.pid = pid; // pid + p_proc->task.stat = IDLE; // 状态 + /**************LDT*********************************/ p_proc->task.ldt_sel = selector_ldt; - memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[0], &gdt[SELECTOR_KERNEL_CS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[0].attr1 = DA_C | PRIVILEGE_USER << 5; - memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3],sizeof(DESCRIPTOR)); + memcpy(&p_proc->task.ldts[1], &gdt[SELECTOR_KERNEL_DS >> 3], sizeof(DESCRIPTOR)); p_proc->task.ldts[1].attr1 = DA_DRW | PRIVILEGE_USER << 5; - + /**************寄存器初值**********************************/ - p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_USER; - p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_USER; - p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_USER; - p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_USER; - p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK)| SA_TIL | RPL_USER; - p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK)| RPL_USER; + p_proc->task.regs.cs = ((8 * 0) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_USER; + p_proc->task.regs.ds = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_USER; + p_proc->task.regs.es = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_USER; + p_proc->task.regs.fs = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_USER; + p_proc->task.regs.ss = ((8 * 1) & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_USER; + p_proc->task.regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK) | RPL_USER; p_proc->task.regs.eflags = 0x0202; /* IF=1, 倒数第二位恒为1 */ - + /****************页表、代码数据、堆栈*****************************/ - //无 - + // 无 + /***************copy registers data to kernel stack****************************/ - //copy registers data to the bottom of the new kernel stack - //added by xw, 17/12/11 - p_regs = (char*)(p_proc + 1); + // copy registers data to the bottom of the new kernel stack + // added by xw, 17/12/11 + p_regs = (char *)(p_proc + 1); p_regs -= P_STACKTOP; - memcpy(p_regs, (char*)p_proc, 18 * 4); - + memcpy(p_regs, (char *)p_proc, 18 * 4); + /***************some field about process switch****************************/ - p_proc->task.esp_save_int = p_regs; //initialize esp_save_int, added by xw, 17/12/11 - //p_proc->task.save_type = 1; - p_proc->task.esp_save_context = p_regs - 10 * 4; //when the process is chosen to run for the first time, - //sched() will fetch value from esp_save_context + p_proc->task.esp_save_int = p_regs; // initialize esp_save_int, added by xw, 17/12/11 + // p_proc->task.save_type = 1; + p_proc->task.esp_save_context = p_regs - 10 * 4; // when the process is chosen to run for the first time, + // sched() will fetch value from esp_save_context eip_context = restart_restore; - *(u32*)(p_regs - 4) = (u32)eip_context; //initialize EIP in the context, so the process can - //start run. added by xw, 18/4/18 - *(u32*)(p_regs - 8) = 0x1202; //initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 - + *(u32 *)(p_regs - 4) = (u32)eip_context; // initialize EIP in the context, so the process can + // start run. added by xw, 18/4/18 + *(u32 *)(p_regs - 8) = 0x1202; // initialize EFLAGS in the context, IF=1, IOPL=1. xw, 18/4/20 + /***************变量调整****************************/ p_proc++; selector_ldt += 1 << 3; } - - proc_table[0].task.ticks = proc_table[0].task.priority = 1; - proc_table[1].task.ticks = proc_table[1].task.priority = 1; + + proc_table[0].task.ticks = proc_table[0].task.priority = 1; + proc_table[1].task.ticks = proc_table[1].task.priority = 1; proc_table[2].task.ticks = proc_table[2].task.priority = 1; - proc_table[3].task.ticks = proc_table[3].task.priority = 1; //added by xw, 18/8/27 + proc_table[3].task.ticks = proc_table[3].task.priority = 1; // added by xw, 18/8/27 proc_table[NR_K_PCBS].task.ticks = proc_table[NR_K_PCBS].task.priority = 1; - + /* When the first process begin running, a clock-interruption will happen immediately. * If the first process's initial ticks is 1, it won't be the first process to execute its * user code. Thus, it's will look weird, for proc_table[0] don't output first. * added by xw, 18/4/19 */ proc_table[0].task.ticks = 2; - + return 0; } \ No newline at end of file diff --git a/kernel/tty.c b/kernel/tty.c index 177c36e..7226adf 100644 --- a/kernel/tty.c +++ b/kernel/tty.c @@ -13,7 +13,7 @@ #include "stdio.h" -int current_console; //当前显示在屏幕上的console +int current_console; // 当前显示在屏幕上的console void tty_write(TTY *tty, char *buf, int len); int tty_read(TTY *tty, char *buf, int len); @@ -30,13 +30,15 @@ int cur_ntty = 0; static keyboard_buf keyboardbuf[3]; static vga_buf vgabuf[3]; -void init_tty_main() { - NTTY* tty; - for (int i = 0; i < 3; ++ i) { +void init_tty_main() +{ + NTTY *tty; + for (int i = 0; i < 3; ++i) + { tty = &ntty_table[i]; tty->driver_type = 1; // vga - // tty->input_buf = (void*)do_kmalloc(sizeof(keyboard_buf)); - // tty->output_buf = (void*)do_kmalloc(sizeof(vga_buf)); + // tty->input_buf = (void*)do_kmalloc(sizeof(keyboard_buf)); + // tty->output_buf = (void*)do_kmalloc(sizeof(vga_buf)); tty->input_buf = &keyboardbuf[i]; tty->output_buf = &vgabuf[i]; vga_tty_init(tty); @@ -115,17 +117,17 @@ void in_process(TTY *p_tty, u32 key) void task_tty() { - #ifdef DEBUGNEW +#ifdef DEBUGNEW // NTTY* p_tty; // for (p_tty = ntty_table; p_tty < ntty_table + 3; ++ p_tty) { // init_ntty(p_tty); // } - while(1) + while (1) { vga_tty_flush(&ntty_table[cur_ntty]); } - - #else + +#else TTY *p_tty; for (p_tty = TTY_FIRST; p_tty < TTY_END; p_tty++) { @@ -135,7 +137,7 @@ void task_tty() select_console(0); - //设置第一个tty光标位置,第一个tty需要特殊处理 + // 设置第一个tty光标位置,第一个tty需要特殊处理 disable_int(); outb(CRTC_ADDR_REG, CURSOR_H); outb(CRTC_DATA_REG, ((disp_pos / 2) >> 8) & 0xFF); @@ -143,21 +145,21 @@ void task_tty() outb(CRTC_DATA_REG, (disp_pos / 2) & 0xFF); enable_int(); - //轮询 + // 轮询 while (1) { for (p_tty = TTY_FIRST; p_tty < TTY_END; p_tty++) { do { - tty_mouse(p_tty); /* tty判断鼠标操作 */ + tty_mouse(p_tty); /* tty判断鼠标操作 */ tty_dev_read(p_tty); /* 从键盘输入缓冲区读到这个tty自己的缓冲区 */ tty_dev_write(p_tty); /* 把tty缓存区的数据写到这个tty占有的显存 */ } while (p_tty->ibuf_cnt); } } - #endif +#endif } static void init_tty(TTY *p_tty) @@ -184,7 +186,7 @@ static void tty_mouse(TTY *tty) { if (tty->mouse_Y > MOUSE_UPDOWN_BOUND) - { //按住鼠标左键向上滚动 + { // 按住鼠标左键向上滚动 if (tty->console->current_line < 43) { disable_int(); @@ -198,7 +200,7 @@ static void tty_mouse(TTY *tty) } } else if (tty->mouse_Y < -MOUSE_UPDOWN_BOUND) - { //按住鼠标左键向下滚动 + { // 按住鼠标左键向下滚动 if (tty->console->current_line > 0) { disable_int(); @@ -214,7 +216,7 @@ static void tty_mouse(TTY *tty) } if (tty->mouse_mid_button) - { //点击中键复原 + { // 点击中键复原 disable_int(); tty->console->current_line = 0; outb(CRTC_ADDR_REG, START_ADDR_H); @@ -299,7 +301,8 @@ static void put_key(TTY *tty, u32 key) void tty_write(TTY *tty, char *buf, int len) { #ifdef DEBUGNEW - while (--len >= 0) { + while (--len >= 0) + { vga_tty_write(&ntty_table[cur_ntty], *buf++); } // vga_tty_flush(&ntty_table[cur_ntty]); diff --git a/kernel/vfs.c b/kernel/vfs.c index 0eaa2d4..0d10214 100644 --- a/kernel/vfs.c +++ b/kernel/vfs.c @@ -1,6 +1,6 @@ /********************************************************** -* vfs.c //added by mingxuan 2019-5-17 -***********************************************************/ + * vfs.c //added by mingxuan 2019-5-17 + ***********************************************************/ #include "type.h" #include "const.h" @@ -17,21 +17,21 @@ #include "fat32.h" #include "stdio.h" -//static struct device device_table[NR_DEV]; //deleted by mingxuan 2020-10-18 -static struct vfs vfs_table[NR_FS]; //modified by mingxuan 2020-10-18 +// static struct device device_table[NR_DEV]; //deleted by mingxuan 2020-10-18 +static struct vfs vfs_table[NR_FS]; // modified by mingxuan 2020-10-18 struct file_desc f_desc_table[NR_FILE_DESC]; -struct super_block super_block[NR_SUPER_BLOCK]; //added by mingxuan 2020-10-30 +struct super_block super_block[NR_SUPER_BLOCK]; // added by mingxuan 2020-10-30 -//static struct file_op f_op_table[NR_fs]; //文件系统操作表 -static struct file_op f_op_table[NR_FS_OP]; //modified by mingxuan 2020-10-18 -static struct sb_op sb_op_table[NR_SB_OP]; //added by mingxuan 2020-10-30 +// static struct file_op f_op_table[NR_fs]; //文件系统操作表 +static struct file_op f_op_table[NR_FS_OP]; // modified by mingxuan 2020-10-18 +static struct sb_op sb_op_table[NR_SB_OP]; // added by mingxuan 2020-10-30 -//static void init_dev_table();//deleted by mingxuan 2020-10-30 -static void init_vfs_table(); //modified by mingxuan 2020-10-30 -void init_file_desc_table(); //added by mingxuan 2020-10-30 +// static void init_dev_table();//deleted by mingxuan 2020-10-30 +static void init_vfs_table(); // modified by mingxuan 2020-10-30 +void init_file_desc_table(); // added by mingxuan 2020-10-30 void init_fileop_table(); -void init_super_block_table(); //added by mingxuan 2020-10-30 +void init_super_block_table(); // added by mingxuan 2020-10-30 void init_sb_op_table(); static int get_index(char path[]); @@ -41,25 +41,25 @@ void init_vfs() init_file_desc_table(); init_fileop_table(); - - init_super_block_table(); - init_sb_op_table(); //added by mingxuan 2020-10-30 - //init_dev_table(); //deleted by mingxuan 2020-10-30 - init_vfs_table(); //modified by mingxuan 2020-10-30 + init_super_block_table(); + init_sb_op_table(); // added by mingxuan 2020-10-30 + + // init_dev_table(); //deleted by mingxuan 2020-10-30 + init_vfs_table(); // modified by mingxuan 2020-10-30 } -//added by mingxuan 2020-10-30 +// added by mingxuan 2020-10-30 void init_file_desc_table() { int i; - for (i = 0; i < NR_FILE_DESC; i++) - memset(&f_desc_table[i], 0, sizeof(struct file_desc)); + for (i = 0; i < NR_FILE_DESC; i++) + memset(&f_desc_table[i], 0, sizeof(struct file_desc)); } void init_fileop_table() { - // table[0] for tty + // table[0] for tty f_op_table[0].open = real_open; f_op_table[0].close = real_close; f_op_table[0].write = real_write; @@ -67,7 +67,7 @@ void init_fileop_table() f_op_table[0].unlink = real_unlink; f_op_table[0].read = real_read; - // table[1] for orange + // table[1] for orange f_op_table[1].open = real_open; f_op_table[1].close = real_close; f_op_table[1].write = real_write; @@ -85,130 +85,136 @@ void init_fileop_table() f_op_table[2].opendir = OpenDir; f_op_table[2].createdir = CreateDir; f_op_table[2].deletedir = DeleteDir; - } -//added by mingxuan 2020-10-30 -void init_super_block_table(){ - struct super_block * sb = super_block; //deleted by mingxuan 2020-10-30 +// added by mingxuan 2020-10-30 +void init_super_block_table() +{ + struct super_block *sb = super_block; // deleted by mingxuan 2020-10-30 - //super_block[0] is tty0, super_block[1] is tty1, uper_block[2] is tty2 - for(; sb < &super_block[3]; sb++) { - sb->sb_dev = DEV_CHAR_TTY; + // super_block[0] is tty0, super_block[1] is tty1, uper_block[2] is tty2 + for (; sb < &super_block[3]; sb++) + { + sb->sb_dev = DEV_CHAR_TTY; sb->fs_type = TTY_FS_TYPE; } - //super_block[3] is orange's superblock + // super_block[3] is orange's superblock sb->sb_dev = DEV_HD; - sb->fs_type = ORANGE_TYPE; + sb->fs_type = ORANGE_TYPE; sb++; - //super_block[4] is fat32's superblock + // super_block[4] is fat32's superblock sb->sb_dev = DEV_HD; - sb->fs_type = FAT32_TYPE; + sb->fs_type = FAT32_TYPE; sb++; - //another super_block are free - for (; sb < &super_block[NR_SUPER_BLOCK]; sb++) { - sb->sb_dev = NO_DEV; + // another super_block are free + for (; sb < &super_block[NR_SUPER_BLOCK]; sb++) + { + sb->sb_dev = NO_DEV; sb->fs_type = NO_FS_TYPE; - } + } } -//added by mingxuan 2020-10-30 -void init_sb_op_table(){ - //orange +// added by mingxuan 2020-10-30 +void init_sb_op_table() +{ + // orange sb_op_table[0].read_super_block = read_super_block; sb_op_table[0].get_super_block = get_super_block; - //fat32 and tty + // fat32 and tty sb_op_table[1].read_super_block = NULL; sb_op_table[1].get_super_block = NULL; } -//static void init_dev_table(){ -static void init_vfs_table(){ // modified by mingxuan 2020-10-30 +// static void init_dev_table(){ +static void init_vfs_table() +{ // modified by mingxuan 2020-10-30 // 我们假设每个tty就是一个文件系统 // tty0 // device_table[0].dev_name="dev_tty0"; // device_table[0].op = &f_op_table[0]; - vfs_table[0].fs_name = "dev_tty0"; //modifed by mingxuan 2020-10-18 + vfs_table[0].fs_name = "dev_tty0"; // modifed by mingxuan 2020-10-18 vfs_table[0].op = &f_op_table[0]; - vfs_table[0].sb = &super_block[0]; //每个tty都有一个superblock //added by mingxuan 2020-10-30 - vfs_table[0].s_op = &sb_op_table[1]; //added by mingxuan 2020-10-30 + vfs_table[0].sb = &super_block[0]; // 每个tty都有一个superblock //added by mingxuan 2020-10-30 + vfs_table[0].s_op = &sb_op_table[1]; // added by mingxuan 2020-10-30 // tty1 - //device_table[1].dev_name="dev_tty1"; - //device_table[1].op =&f_op_table[0]; - vfs_table[1].fs_name = "dev_tty1"; //modifed by mingxuan 2020-10-18 + // device_table[1].dev_name="dev_tty1"; + // device_table[1].op =&f_op_table[0]; + vfs_table[1].fs_name = "dev_tty1"; // modifed by mingxuan 2020-10-18 vfs_table[1].op = &f_op_table[0]; - vfs_table[1].sb = &super_block[1]; //每个tty都有一个superblock //added by mingxuan 2020-10-30 - vfs_table[1].s_op = &sb_op_table[1]; //added by mingxuan 2020-10-30 + vfs_table[1].sb = &super_block[1]; // 每个tty都有一个superblock //added by mingxuan 2020-10-30 + vfs_table[1].s_op = &sb_op_table[1]; // added by mingxuan 2020-10-30 // tty2 - //device_table[2].dev_name="dev_tty2"; - //device_table[2].op=&f_op_table[0]; - vfs_table[2].fs_name = "dev_tty2"; //modifed by mingxuan 2020-10-18 + // device_table[2].dev_name="dev_tty2"; + // device_table[2].op=&f_op_table[0]; + vfs_table[2].fs_name = "dev_tty2"; // modifed by mingxuan 2020-10-18 vfs_table[2].op = &f_op_table[0]; - vfs_table[2].sb = &super_block[2]; //每个tty都有一个superblock //added by mingxuan 2020-10-30 - vfs_table[2].s_op = &sb_op_table[1]; //added by mingxuan 2020-10-30 + vfs_table[2].sb = &super_block[2]; // 每个tty都有一个superblock //added by mingxuan 2020-10-30 + vfs_table[2].s_op = &sb_op_table[1]; // added by mingxuan 2020-10-30 // fat32 - //device_table[3].dev_name="fat0"; - //device_table[3].op=&f_op_table[2]; - vfs_table[3].fs_name = "fat0"; //modifed by mingxuan 2020-10-18 + // device_table[3].dev_name="fat0"; + // device_table[3].op=&f_op_table[2]; + vfs_table[3].fs_name = "fat0"; // modifed by mingxuan 2020-10-18 vfs_table[3].op = &f_op_table[2]; - vfs_table[3].sb = &super_block[4]; //added by mingxuan 2020-10-30 - vfs_table[3].s_op = &sb_op_table[1]; //added by mingxuan 2020-10-30 + vfs_table[3].sb = &super_block[4]; // added by mingxuan 2020-10-30 + vfs_table[3].s_op = &sb_op_table[1]; // added by mingxuan 2020-10-30 // orange - //device_table[4].dev_name="orange"; - //device_table[4].op=&f_op_table[1]; - vfs_table[4].fs_name = "orange"; //modifed by mingxuan 2020-10-18 + // device_table[4].dev_name="orange"; + // device_table[4].op=&f_op_table[1]; + vfs_table[4].fs_name = "orange"; // modifed by mingxuan 2020-10-18 vfs_table[4].op = &f_op_table[1]; - vfs_table[4].sb = &super_block[3]; //added by mingxuan 2020-10-30 - vfs_table[4].s_op = &sb_op_table[0]; //added by mingxuan 2020-10-30 - + vfs_table[4].sb = &super_block[3]; // added by mingxuan 2020-10-30 + vfs_table[4].s_op = &sb_op_table[0]; // added by mingxuan 2020-10-30 } -static int get_index(char path[]){ +static int get_index(char path[]) +{ int pathlen = strlen(path); - //char dev_name[DEV_NAME_LEN]; - char fs_name[DEV_NAME_LEN]; //modified by mingxuan 2020-10-18 + // char dev_name[DEV_NAME_LEN]; + char fs_name[DEV_NAME_LEN]; // modified by mingxuan 2020-10-18 int len = (pathlen < DEV_NAME_LEN) ? pathlen : DEV_NAME_LEN; - - int i,a=0; - for(i=0;iopen(pathname, flags); //modified by mingxuan 2020-10-18 - if(fd != -1) + fd = vfs_table[index].op->open(pathname, flags); // modified by mingxuan 2020-10-18 + if (fd != -1) + { + p_proc_current->task.filp[fd]->dev_index = index; + } + else { - p_proc_current -> task.filp[fd] -> dev_index = index; - } else { kprintf(" error!\n"); } - - return fd; + + return fd; } - -int do_vclose(int fd) { +int do_vclose(int fd) +{ int index = p_proc_current->task.filp[fd]->dev_index; - return vfs_table[index].op->close(fd); //modified by mingxuan 2020-10-18 + return vfs_table[index].op->close(fd); // modified by mingxuan 2020-10-18 } -int do_vread(int fd, char *buf, int count) { +int do_vread(int fd, char *buf, int count) +{ int index = p_proc_current->task.filp[fd]->dev_index; - return vfs_table[index].op->read(fd, buf, count); //modified by mingxuan 2020-10-18 + return vfs_table[index].op->read(fd, buf, count); // modified by mingxuan 2020-10-18 } -int do_vwrite(int fd, const char *buf, int count) { - //modified by mingxuan 2019-5-23 +int do_vwrite(int fd, const char *buf, int count) +{ + // modified by mingxuan 2019-5-23 char s[512]; int index = p_proc_current->task.filp[fd]->dev_index; const char *fsbuf = buf; int f_len = count; int bytes; - while(f_len) + while (f_len) { int iobytes = min(512, f_len); - int i=0; - for(i=0; iwrite(fd,s,iobytes); - bytes = vfs_table[index].op->write(fd,s,iobytes); //modified by mingxuan 2020-10-18 - if(bytes != iobytes) + // bytes = device_table[index].op->write(fd,s,iobytes); + bytes = vfs_table[index].op->write(fd, s, iobytes); // modified by mingxuan 2020-10-18 + if (bytes != iobytes) { return bytes; // TODO: Maybe problematic } @@ -332,146 +349,167 @@ int do_vwrite(int fd, const char *buf, int count) { return count; } -int do_vunlink(const char *path) { +int do_vunlink(const char *path) +{ int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,(char *)path); + + strcpy(pathname, (char *)path); pathname[pathlen] = 0; int index; index = get_index(pathname); - if(index==-1){ + if (index == -1) + { kprintf("pathname error!\n"); return -1; } - - //return device_table[index].op->unlink(pathname); - return vfs_table[index].op->unlink(pathname); //modified by mingxuan 2020-10-18 + + // return device_table[index].op->unlink(pathname); + return vfs_table[index].op->unlink(pathname); // modified by mingxuan 2020-10-18 } -int do_vlseek(int fd, int offset, int whence) { +int do_vlseek(int fd, int offset, int whence) +{ int index = p_proc_current->task.filp[fd]->dev_index; - //return device_table[index].op->lseek(fd, offset, whence); - return vfs_table[index].op->lseek(fd, offset, whence); //modified by mingxuan 2020-10-18 - + // return device_table[index].op->lseek(fd, offset, whence); + return vfs_table[index].op->lseek(fd, offset, whence); // modified by mingxuan 2020-10-18 } -//int do_vcreate(char *pathname) { -int do_vcreate(char *filepath) { //modified by mingxuan 2019-5-17 - //added by mingxuan 2019-5-17 +// int do_vcreate(char *pathname) { +int do_vcreate(char *filepath) +{ // modified by mingxuan 2019-5-17 + // added by mingxuan 2019-5-17 int state; const char *path = filepath; int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,(char *)path); + + strcpy(pathname, (char *)path); pathname[pathlen] = 0; int index; index = get_index(pathname); - if(index == -1){ + if (index == -1) + { kprintf("pathname error! path: %s\n", path); return -1; } - state = vfs_table[index].op->create(pathname); //modified by mingxuan 2020-10-18 - if (state == 1) { + state = vfs_table[index].op->create(pathname); // modified by mingxuan 2020-10-18 + if (state == 1) + { kprintf(" create file success!"); - } else { - DisErrorInfo(state); + } + else + { + DisErrorInfo(state); } return state; } -int do_vdelete(char *path) { +int do_vdelete(char *path) +{ int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,path); + + strcpy(pathname, path); pathname[pathlen] = 0; int index; index = get_index(pathname); - if(index==-1){ + if (index == -1) + { kprintf("pathname error!\n"); return -1; } - //return device_table[index].op->delete(pathname); - return vfs_table[index].op->delete(pathname); //modified by mingxuan 2020-10-18 + // return device_table[index].op->delete(pathname); + return vfs_table[index].op->delete (pathname); // modified by mingxuan 2020-10-18 } -int do_vopendir(char *path) { +int do_vopendir(char *path) +{ int state; int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,path); + + strcpy(pathname, path); pathname[pathlen] = 0; int index; - index = (int)(pathname[1]-'0'); + index = (int)(pathname[1] - '0'); - for(int j=0;j<= pathlen-3;j++) + for (int j = 0; j <= pathlen - 3; j++) { - pathname[j] = pathname[j+3]; + pathname[j] = pathname[j + 3]; } state = f_op_table[index].opendir(pathname); - if (state == 1) { + if (state == 1) + { kprintf(" open dir success!"); - } else { - DisErrorInfo(state); - } + } + else + { + DisErrorInfo(state); + } return state; } -int do_vcreatedir(char *path) { +int do_vcreatedir(char *path) +{ int state; int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,path); + + strcpy(pathname, path); pathname[pathlen] = 0; int index; - index = (int)(pathname[1]-'0'); + index = (int)(pathname[1] - '0'); - for(int j=0;j<= pathlen-3;j++) + for (int j = 0; j <= pathlen - 3; j++) { - pathname[j] = pathname[j+3]; + pathname[j] = pathname[j + 3]; } state = f_op_table[index].createdir(pathname); - if (state == 1) { + if (state == 1) + { kprintf(" create dir success!"); - } else { - DisErrorInfo(state); - } + } + else + { + DisErrorInfo(state); + } return state; } -int do_vdeletedir(char *path) { +int do_vdeletedir(char *path) +{ int state; int pathlen = strlen(path); char pathname[MAX_PATH]; - - strcpy(pathname,path); + + strcpy(pathname, path); pathname[pathlen] = 0; int index; - index = (int)(pathname[1]-'0'); + index = (int)(pathname[1] - '0'); - for(int j=0;j<= pathlen-3;j++) + for (int j = 0; j <= pathlen - 3; j++) { - pathname[j] = pathname[j+3]; + pathname[j] = pathname[j + 3]; } state = f_op_table[index].deletedir(pathname); - if (state == 1) { + if (state == 1) + { kprintf(" delete dir success!"); - } else { - DisErrorInfo(state); - } + } + else + { + DisErrorInfo(state); + } return state; } \ No newline at end of file diff --git a/kernel/vga.c b/kernel/vga.c index 304f43b..5edf2bb 100644 --- a/kernel/vga.c +++ b/kernel/vga.c @@ -19,7 +19,7 @@ /***************************************************************************** * Display the cursor by setting CRTC (6845 compatible) registers. - * + * * @param position Position of the cursor based on the beginning of the video * memory. Note that it counts in WORDs, not in BYTEs. *****************************************************************************/ @@ -43,14 +43,14 @@ 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); } /***************************************************************************** * Routine for hardware screen scrolling. - * + * * @param addr Offset in the video memory. *****************************************************************************/ static inline void vga_set_video_start_addr(u32 addr) @@ -63,52 +63,56 @@ static inline void vga_set_video_start_addr(u32 addr) enable_int(); } - /***************************************************************************** * 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); +static inline void vga_put_raw(u32 pos, u16 dat) +{ + u16 *pch = (u16 *)K_PHY2LIN(V_MEM_BASE + pos); *pch = dat; } - /***************************************************************************** * copy a whole screen of text mode data into video memory, assume that screen * width is 80 or 40 - * + * * @param src memory block with the same size as text mode video memory() *****************************************************************************/ -static inline void vga_flush_screen(void* src) { - u32* _src = src; - u32* dst = (u32*)K_PHY2LIN(V_MEM_BASE); - for (int i = 0; i < SCR_SIZE * sizeof(u16) / sizeof(u32); ++ i) { - *dst ++ = *_src ++; - } -} - - -/***************************************************************************** - * copy a whole screen of text mode data into video memory, assume that screen - * width is 80 or 40 - * - * @param src memory block with the same size as text mode video memory() - *****************************************************************************/ -static inline void vga_flush_line(void* src, int line_no) { - u32* _src = src; - 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 ++ = *_src ++; +static inline void vga_flush_screen(void *src) +{ + u32 *_src = src; + u32 *dst = (u32 *)K_PHY2LIN(V_MEM_BASE); + for (int i = 0; i < SCR_SIZE * sizeof(u16) / sizeof(u32); ++i) + { + *dst++ = *_src++; } } -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; +/***************************************************************************** + * copy a whole screen of text mode data into video memory, assume that screen + * width is 80 or 40 + * + * @param src memory block with the same size as text mode video memory() + *****************************************************************************/ +static inline void vga_flush_line(void *src, int line_no) +{ + u32 *_src = src; + 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++ = *_src++; + } +} + +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; } } @@ -121,7 +125,7 @@ void vga_tty_init(NTTY* tty) { static int _cnt = 0; assert(tty->driver_type == 1); assert(tty->output_buf); - vga_buf* vga = tty->output_buf; + vga_buf *vga = tty->output_buf; // vga->buf = (void*)do_kmalloc(sizeof(u16) * SCR_BUFSIZE); vga->buf = (void*)pagebuf[_cnt ++]; // kprintf("malloced %p %p %p\n", vga->buf, &vga->buf, &vga->scr_top_line); @@ -129,66 +133,314 @@ void vga_tty_init(NTTY* tty) { // buf->max_line = SCR_BUFSIZE / SCR_WIDTH; vga->scr_top_line = vga->scr_cur_line = 0; vga->head_line = 0; - u32* ptr_buf = (u32*) vga->buf; - for (int i = 0; i < SCR_BUFSIZE * sizeof(u16) / sizeof(u32); ++i) { + u32 *ptr_buf = (u32 *)vga->buf; + for (int i = 0; i < SCR_BUFSIZE * sizeof(u16) / sizeof(u32); ++i) + { ptr_buf[i] = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space } - kprintf("%p 0x%x %d\n", vga->buf, ((u32*)vga->buf)[20], vga->scr_cur_line); - + kprintf("%p 0x%x %d\n", vga->buf, ((u32 *)vga->buf)[20], vga->scr_cur_line); } -#define INDEX(row, col) ((row) * SCR_WIDTH + (col)) -#define NEXTLINE(row) NEXT(row, SCR_MAXLINE) -#define LASTLINE(row) LAST(row, SCR_MAXLINE) +#define INDEX(row, col) ((row)*SCR_WIDTH + (col)) +#define NEXTLINE(row) (((row) + 1) % SCR_MAXLINE) +#define LASTLINE(row) (((row)-1) >= 0 ? ((row)-1) % SCR_MAXLINE : SCR_MAXLINE) -static void newline(vga_buf* vga) { - vga->cur_col = 0; +static void newline(vga_buf *vgabuf) +{ + vgabuf->cur_col = 0; // kprintf("bf %x\n", vgabuf->scr_cur_line); - vga->scr_cur_line = NEXTLINE(vga->scr_cur_line); + vgabuf->scr_cur_line = NEXTLINE(vgabuf->scr_cur_line); // kprintf("af %x\n", vgabuf->scr_cur_line); - vga->cur_row = CYCLE_SUB(vga->scr_top_line, vga->scr_cur_line, SCR_MAXLINE); - if (vga->cur_row == SCR_HEIGHT) { + vgabuf->cur_row = abs(vgabuf->scr_cur_line - vgabuf->scr_top_line); + if (vgabuf->cur_row == SCR_HEIGHT) + { // auto scroll - vga->scr_top_line = NEXTLINE(vga->scr_top_line); - if(vga->scr_cur_line == vga->head_line) { - vga->head_line = NEXTLINE(vga->head_line); + vgabuf->scr_top_line = NEXTLINE(vgabuf->scr_top_line); + if (vgabuf->scr_cur_line == vgabuf->head_line) + { + vgabuf->head_line = NEXTLINE(vgabuf->head_line); // remember to fill blank the old line - u32* ptr_buf = (u32*) (vga->buf + sizeof(u16) * vga->head_line * SCR_WIDTH); - for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i) { + u32 *ptr_buf = (u32 *)(vgabuf->buf + sizeof(u16) * vgabuf->head_line * SCR_WIDTH); + for (int i = 0; i < SCR_WIDTH * sizeof(u16) / sizeof(u32); ++i) + { *ptr_buf++ = (BLANK << 16) | BLANK; // bg-black, fg-white, ascii-space } } } } -void vga_tty_write(NTTY* tty, char ch) { +static void nextcol(vga_buf *vgabuf) +{ + vgabuf->cur_col++; + if (vgabuf->cur_col == SCR_WIDTH) + { + newline(vgabuf); + } +} + +static void cursor_move(i16 move_row, i16 move_col, vga_buf *vgabuf) +{ + vgabuf->scr_cur_line += move_row; + if (vgabuf->scr_cur_line < 0) + vgabuf->scr_cur_line = 0; + else if (vgabuf->scr_cur_line >= SCR_MAXLINE) + vgabuf->scr_cur_line = SCR_MAXLINE - 1; + + vgabuf->cur_col += move_col; + if (vgabuf->cur_col < 0) + vgabuf->cur_col = 0; + else if (vgabuf->cur_col >= SCR_WIDTH) + vgabuf->cur_col = SCR_WIDTH - 1; +} + +inline static void +param12vga_color(i16 *param) +{ + u8 tmp = *param & 1; + *param &= 0b0110; + *param |= *param >> 2; + *param &= 0b0011; + *param |= tmp << 2; +} + +static void set_color(vga_buf *vgabuf) +{ + if (vgabuf->param1 == 0) + { + vgabuf->color = DEFAULT_CHAR_COLOR; + } + else if (vgabuf->param1 == 1) + { + vgabuf->color |= 0x8800; + } + else if (vgabuf->param1 == 2) + { + vgabuf->color &= 0x7700; + } + else if (30 <= vgabuf->param1 && vgabuf->param1 <= 37) + { + vgabuf->param1 -= 30; + param12vga_color(&(vgabuf->param1)); + vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param1); + } + else if (40 <= vgabuf->param1 && vgabuf->param1 <= 47) + { + vgabuf->param1 -= 40; + param12vga_color(&(vgabuf->param1)); + vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param1); + } + else if (90 <= vgabuf->param1 && vgabuf->param1 <= 97) + { + vgabuf->param1 -= 90; + param12vga_color(&(vgabuf->param1)); + vgabuf->param1 |= 0x8; + vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param1); + } + else if (100 <= vgabuf->param1 && vgabuf->param1 <= 107) + { + vgabuf->param1 -= 100; + param12vga_color(&(vgabuf->param1)); + vgabuf->param1 |= 0x8; + vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param1); + } + else + { + warn("unsupport CSI: color"); + } + + if (vgabuf->param2 == 0 && vgabuf->param1 == 0) + { + vgabuf->color = DEFAULT_CHAR_COLOR; + } + else if (vgabuf->param2 == 1) + { + vgabuf->color |= 0x8800; + } + else if (vgabuf->param2 == 2) + { + vgabuf->color &= 0x7700; + } + else if (30 <= vgabuf->param2 && vgabuf->param2 <= 37) + { + vgabuf->param2 -= 30; + param12vga_color(&(vgabuf->param2)); + vgabuf->color = (vgabuf->color & 0xf8ff) | FOREGROUND(vgabuf->param2); + } + else if (40 <= vgabuf->param2 && vgabuf->param2 <= 47) + { + vgabuf->param2 -= 40; + param12vga_color(&(vgabuf->param2)); + vgabuf->color = (vgabuf->color & 0x8fff) | BACKGROUND(vgabuf->param2); + } + else if (90 <= vgabuf->param2 && vgabuf->param2 <= 97) + { + vgabuf->param2 -= 90; + param12vga_color(&(vgabuf->param2)); + vgabuf->param2 |= 0x8; + vgabuf->color = (vgabuf->color & 0xf0ff) | FOREGROUND(vgabuf->param2); + } + else if (100 <= vgabuf->param2 && vgabuf->param2 <= 107) + { + vgabuf->param2 -= 100; + param12vga_color(&(vgabuf->param2)); + vgabuf->param2 |= 0x8; + vgabuf->color = (vgabuf->color & 0x0fff) | BACKGROUND(vgabuf->param2); + } + else + { + warn("unsupport CSI: color"); + } +} + +static void CSI_handler(u8 terminator, vga_buf *vgabuf) +{ + vgabuf->CSI = CSI_ESC; + switch (terminator) + { + case 'A': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(-vgabuf->param1, 0, vgabuf); + break; + case 'B': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(+vgabuf->param1, 0, vgabuf); + break; + case 'C': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(0, -vgabuf->param1, vgabuf); // nothing + break; + case 'D': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(0, +vgabuf->param1, vgabuf); + break; + case 'E': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(+vgabuf->param1, -vgabuf->cur_col, vgabuf); + break; + case 'F': + if (vgabuf->param1 == 0) + vgabuf->param1 == 1; + cursor_move(-vgabuf->param1, -vgabuf->cur_col, vgabuf); + break; + case 'G': // added + cursor_move(0, vgabuf->param1 - vgabuf->cur_col, vgabuf); + break; + case 'H': + cursor_move(vgabuf->param1 - vgabuf->scr_cur_line, vgabuf->param1 - vgabuf->cur_col, vgabuf); + break; + case 'J': + break; + case 'K': + break; + case 'S': + break; + case 'T': + break; + case 'm': + set_color(vgabuf); + break; + } +} + +void vga_tty_write(NTTY *tty, char ch) +{ // assert(tty->driver_type == 1); // assert(tty->output_buf); - vga_buf* vga = tty->output_buf; - u16* buf = vga->buf; + vga_buf *vga = tty->output_buf; + u16 *buf = vga->buf; // kprintf("vga_tty_write %c to %d %d\n", ch, vga->scr_cur_line, vga->cur_col); - switch (ch) + if (vga->CSI == CSI_ESC) { - case '\n': - newline(vga); - break; - case '\r': - vga->cur_col = 0; - break; - case '\b': - // this implementation is mimic to usual linux shell - // it moves cursor left but neither crosses line nor delete character - if (vga->cur_col > 0) { - vga->cur_col --; - } - break; - default: - buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(DEFAULT_CHAR_COLOR, ch); - vga->cur_col ++; - if (vga->cur_col == SCR_WIDTH) { + switch (ch) + { + case '\t': + if (INDEX(vga->scr_cur_line, vga->cur_col) == SCR_BUFSIZE - 1) + break; + while (vga->cur_col % 4 != 1) + { + nextcol(vga); + } + break; + case '\n': newline(vga); + break; + case '\r': + vga->cur_col = 0; + break; + case '\b': + // this implementation is mimic to usual linux shell + // it moves cursor left but neither crosses line nor delete character + if (vga->cur_col > 0) + { + vga->cur_col--; + } + break; + case '\x1b': + vga->CSI = CSI_BRACKET; + break; + default: + if (vga->color == 0) + { + buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(DEFAULT_CHAR_COLOR, ch); + } + else + { + buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(vga->color, ch); + } + // buf[INDEX(vga->scr_cur_line, vga->cur_col)] = MAKE_CELL(DEFAULT_CHAR_COLOR, ch); + nextcol(vga); + break; + } + } + else if (vga->CSI == CSI_BRACKET) + { + switch (ch) + { + case '[': + vga->CSI = CSI_PARAM1; + vga->param1 = vga->param2 = 0; + break; + default: + vga->CSI = CSI_ESC; + break; + } + } + else + { + switch (ch) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (vga->CSI == CSI_PARAM1) + vga->param1 = vga->param1 * 10 + ch - '0'; + else if (vga->CSI == CSI_PARAM2) + vga->param2 = vga->param2 * 10 + ch - '0'; + else + ; // do nothing + break; + case ';': + vga->CSI = CSI_PARAM2; + break; + default: + if (!(0x20 <= ch && ch <= 0x7e)) + vga->CSI = CSI_ESC; + if (0x40 <= ch && ch <= 0x7e) + CSI_handler(ch, vga); + break; } - break; } vga->cur_row = CYCLE_SUB(vga->scr_top_line, vga->scr_cur_line, SCR_MAXLINE); // kprintf("row: %d; ", vga->cur_row); @@ -212,49 +464,69 @@ void vga_tty_flush(NTTY* tty) { u16* buf = vga->buf; int i, cur_line; vga_set_cursor(INDEX(vga->cur_row, vga->cur_col)); - if (vga->cur_row == SCR_WIDTH - 1) { + if (vga->cur_row == SCR_WIDTH - 1) + { vga_flush_screen(&buf[INDEX(vga->scr_top_line, 0)]); } - else { + else + { cur_line = vga->scr_top_line; - for (i = 0; i <= vga->cur_row; ++ i){ + for (i = 0; i <= vga->cur_row; ++i) + { vga_flush_line(&buf[INDEX(cur_line, 0)], i); cur_line = NEXTLINE(cur_line); } - for (; i < SCR_WIDTH; ++ i) { + for (; i < SCR_WIDTH; ++i) + { vga_flush_blankline(i); } } // kprintf("flush: row=%d, top=%d, cur=%d\n", vga->cur_row, vga->scr_top_line, vga->scr_cur_line); } -void vga_tty_scroll(NTTY* tty, int direction) { - vga_buf* vga = tty->output_buf; - u16* buf = vga->buf; - if (direction > 0) { +void vga_tty_scroll(NTTY *tty, int direction) +{ + vga_buf *vga = tty->output_buf; + u16 *buf = vga->buf; + if (direction > 0) + { // down - if (vga->scr_top_line == vga->scr_cur_line) return; + if (vga->scr_top_line == vga->scr_cur_line) + return; vga->scr_top_line = NEXTLINE(vga->scr_top_line); } - else { - if (vga->scr_top_line == vga->head_line) return; + else + { + if (vga->scr_top_line == vga->head_line) + return; vga->scr_top_line = LASTLINE(vga->scr_top_line); } vga->cur_row = abs(vga->scr_cur_line - vga->scr_top_line); - if (vga->cur_row >= SCR_HEIGHT) { + if (vga->cur_row >= SCR_HEIGHT) + { vga_disable_cursor(); } - else { + else + { vga_enable_cursor(0, 15); } } -void vga_scroll_to_cur(NTTY* tty) { - vga_buf* vga = tty->output_buf; - u16* buf = vga->buf; +void vga_scroll_to_cur(NTTY *tty) +{ + vga_buf *vga = tty->output_buf; + u16 *buf = vga->buf; // vga->scr_top_line = vga->scr_cur_line } -void vga_tty_select(NTTY* tty) { +void vga_ttroll_to_cur(NTTY *tty) +{ + vga_buf *vga = tty->output_buf; + u16 *buf = vga->buf; + // vga->scr_top_line = vga->scr_cur_line +} + +void vga_tty_select(NTTY *tty) +{ // } \ No newline at end of file diff --git a/user/test.c b/user/test.c index d743d74..90d1883 100644 --- a/user/test.c +++ b/user/test.c @@ -16,11 +16,14 @@ int main(int arg, char *argv[]) char buf[1024]; int pid; int times = 0; - for (int i = 0; i < 20; ++ i) { - printf("test %d\n", i); + for (int i = 0; i < 20; ++i) + { + printf("test %d\n", i); } - for (int i = 0; i < 2; ++ i) { - printf("1111111111"); + for (int i = 0; i < 2; ++i) + { + printf("\x1b[42;31m1111111111"); + // printf("\x1b[m1111111111"); printf("2222222222"); printf("3333333333"); printf("4444444444"); @@ -31,6 +34,7 @@ int main(int arg, char *argv[]) printf("9999999999\r\b\b\n"); } - while(1); - return 0; + while (1) + ; + return 0; } \ No newline at end of file