From 0f25fbbbc3598e0ef55fdeeb31a15b1bf7ea9b91 Mon Sep 17 00:00:00 2001 From: acevest Date: Mon, 1 Nov 2021 11:59:24 +0800 Subject: [PATCH] code style --- .bochsrc | 1 + .gitignore | 3 + boot/boot.c | 19 +- boot/cmdline.c | 11 +- drivers/console.c | 44 ++--- drivers/console.h | 2 +- drivers/ide.c | 220 +++++++++++------------ drivers/ide.h | 189 ++++++++++---------- drivers/keyboard.c | 422 ++++++++++++++++++++++++++++++++++++++++---- drivers/vga.c | 143 ++++++++------- fs/ext2.c | 112 ++++++------ fs/fs.c | 3 +- fs/read.c | 2 +- fs/write.c | 7 +- include/assert.h | 10 +- include/atomic.h | 9 +- include/bak.ext2.h | 249 +++++++++++++------------- include/bits.h | 78 ++++---- include/boot/boot.h | 23 +-- include/bug.h | 17 +- include/elf.h | 275 ++++++++++++++--------------- include/errno.h | 70 ++++---- include/ext2.h | 227 ++++++++++++------------ include/fcntl.h | 31 ++-- include/fs.h | 53 +++--- include/global.h | 7 +- include/i8259.h | 14 +- include/init.h | 4 +- include/io.h | 107 +++++------ include/irq.h | 54 +++--- include/linkage.h | 14 +- include/list.h | 53 +++--- include/mm.h | 1 - include/msr.h | 17 +- include/page.h | 121 ++++++------- include/pci.h | 127 +++++++------ include/printk.h | 10 +- include/processor.h | 217 +++++++++++------------ include/semaphore.h | 12 +- include/stat.h | 68 +++---- include/stdio.h | 4 +- include/stdlib.h | 4 +- include/string.h | 9 +- include/syscall.h | 16 +- include/system.h | 282 ++++++++++++++--------------- include/task.h | 19 +- include/types.h | 69 ++++---- include/unistd.h | 6 +- include/wait.h | 25 ++- kernel/assert.c | 5 +- kernel/clock.c | 2 +- kernel/cpuid.c | 94 +++++----- kernel/exec.c | 81 ++++----- kernel/fork.c | 106 ++++++----- kernel/i8259.c | 49 +++-- kernel/init.c | 22 ++- kernel/innerint.c | 24 +-- kernel/irq.c | 63 +++---- kernel/pci.c | 347 ++++++++++++++++++------------------ kernel/printk.c | 6 +- kernel/sched.c | 87 +++++---- kernel/semaphore.c | 32 ++-- kernel/setup.c | 18 +- kernel/syscall.c | 62 +++---- kernel/system.c | 149 ++++++++-------- kernel/wait.c | 24 ++- lib/fork.c | 4 +- lib/keyboard.c | 90 +++++----- lib/lib.c | 10 +- lib/read.c | 2 +- lib/string.c | 54 +++--- lib/syscall.c | 38 ++-- lib/vsprintf.c | 52 +++--- lib/write.c | 2 +- mm/buddy.c | 72 ++++---- mm/mm.c | 113 ++++++------ mm/page.c | 17 +- mm/slub.c | 81 ++++----- 78 files changed, 2749 insertions(+), 2437 deletions(-) diff --git a/.bochsrc b/.bochsrc index 1fc01d7..9c88cd2 100644 --- a/.bochsrc +++ b/.bochsrc @@ -1308,3 +1308,4 @@ speaker: enabled=1, mode=sound, volume=15 #megs: 32 #megs: 16 #megs: 8 +megs: 32 diff --git a/.gitignore b/.gitignore index b0407b0..1c9ce21 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ *.img *build* *.BIN +*.bin *.map *.diff *.swp @@ -30,3 +31,5 @@ a.* snapshot.txt bochsout.txt *.lock +*.DS_Store +.vscode diff --git a/boot/boot.c b/boot/boot.c index a2e3e74..86f682b 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -10,7 +10,6 @@ *-------------------------------------------------------------------------- */ - #include #include #include @@ -23,7 +22,7 @@ void parse_cmdline(const char *cmdline); void init_boot_params(multiboot_info_t *p) { - boot_params.cmdline = (char *) p->cmdline; + boot_params.cmdline = (char *)p->cmdline; parse_cmdline(boot_params.cmdline); @@ -34,11 +33,11 @@ void init_boot_params(multiboot_info_t *p) boot_params.boot_device = p->boot_device; - memory_map_t *mmap = (memory_map_t *) pa2va(p->mmap_addr); + memory_map_t *mmap = (memory_map_t *)pa2va(p->mmap_addr); unsigned int i; boot_params.e820map.map_cnt = p->mmap_length / sizeof(memory_map_t); - for(i=0; ibase_addr_low; boot_params.e820map.map[i].size = mmap->length_low; @@ -48,18 +47,20 @@ void init_boot_params(multiboot_info_t *p) void check_kernel(unsigned long addr, unsigned long magic) { - if(magic != MULTIBOOT_BOOTLOADER_MAGIC) + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { printk("Your boot loader does not support multiboot.\n"); - while(1); + while (1) + ; } - multiboot_info_t *mbi = (multiboot_info_t *) addr; + multiboot_info_t *mbi = (multiboot_info_t *)addr; - if( (mbi->flags & 0x47) != 0x47) + if ((mbi->flags & 0x47) != 0x47) { printk("Kernel Need More Information\n"); - while(1); + while (1) + ; } init_boot_params(mbi); diff --git a/boot/cmdline.c b/boot/cmdline.c index d75b14e..de21b2e 100644 --- a/boot/cmdline.c +++ b/boot/cmdline.c @@ -18,12 +18,12 @@ static void get_value(const char *name, char *value) { const char *p; - if(0 != (p = strstr(system.cmdline, name)) ) + if (0 != (p = strstr(system.cmdline, name))) { - if(0 != (p = strstr(p, "="))) + if (0 != (p = strstr(p, "="))) { p++; - while(*p != ' ' && *p != 0) + while (*p != ' ' && *p != 0) *value++ = *p++; } } @@ -31,7 +31,6 @@ static void get_value(const char *name, char *value) *value = 0; } - void parse_cmdline(const char *cmdline) { char value[128]; @@ -39,8 +38,8 @@ void parse_cmdline(const char *cmdline) printk("cmdline: %s\n", system.cmdline); get_value("root", value); - assert(value[0]=='h' && value[1]=='d' && value[2] == 'a'); - system.root_dev = MAKE_DEV(DEV_MAJOR_HDA, atoi(value+3)); + assert(value[0] == 'h' && value[1] == 'd' && value[2] == 'a'); + system.root_dev = MAKE_DEV(DEV_MAJOR_HDA, atoi(value + 3)); printk("root device %s [0x%08x]\n", value, system.root_dev); get_value("delay", value); diff --git a/drivers/console.c b/drivers/console.c index 2d78b20..d8c29e5 100644 --- a/drivers/console.c +++ b/drivers/console.c @@ -7,9 +7,9 @@ * ------------------------------------------------------------------------ */ -#include -#include -#include +#include +#include +#include cnsl_t cnsl; @@ -25,7 +25,7 @@ static bool full(const cnsl_queue_t *q) static void put(cnsl_queue_t *q, char c) { - if(!full(q)) + if (!full(q)) { q->data[q->head] = c; q->head = (q->head + 1) % CNSL_QUEUE_SIZE; @@ -34,7 +34,7 @@ static void put(cnsl_queue_t *q, char c) static bool get(cnsl_queue_t *q, char *c) { - if(!empty(q)) + if (!empty(q)) { *c = q->data[q->tail]; q->tail = (q->tail + 1) % CNSL_QUEUE_SIZE; @@ -51,16 +51,15 @@ static void clear(cnsl_queue_t *q) static void erase(cnsl_queue_t *q) { - if(empty(q)) + if (empty(q)) return; - if(q->head == 0) - q->head = CNSL_QUEUE_SIZE -1; + if (q->head == 0) + q->head = CNSL_QUEUE_SIZE - 1; else q->head--; } - static void cnsl_queue_init(cnsl_queue_t *cq) { memset((void *)cq, 0, sizeof(*cq)); @@ -79,15 +78,14 @@ void cnsl_init() init_wait_queue(&rdwq); } - int cnsl_kbd_write(char ch) { - if(ch == 0) + if (ch == 0) return 0; - if(ch == '\b') + if (ch == '\b') { - if(!empty(&cnsl.wr_q)) + if (!empty(&cnsl.wr_q)) vga_putc(0, '\b', 0xF); erase(&cnsl.wr_q); erase(&cnsl.sc_q); @@ -99,45 +97,43 @@ int cnsl_kbd_write(char ch) vga_putc(0, ch, 0xF); } - - if(ch == '\n') + if (ch == '\n') { clear(&cnsl.wr_q); - while(get(&cnsl.sc_q, &ch)) + while (get(&cnsl.sc_q, &ch)) put(&cnsl.rd_q, ch); wake_up(&rdwq); } } - int cnsl_read(char *buf, size_t count) { unsigned long flags; assert(count > 0); int cnt = 0; - for(cnt=0; cntstate = TASK_WAIT; irq_save(flags); bool r = get(&cnsl.rd_q, &ch); irq_restore(flags); - if(r) + if (r) { buf[cnt++] = ch; task->state = TASK_RUNNING; del_wait_queue(&rdwq, &wait); - if(ch == '\n') + if (ch == '\n') goto end; break; @@ -145,7 +141,6 @@ int cnsl_read(char *buf, size_t count) schedule(); } - } end: @@ -154,5 +149,4 @@ end: } chrdev_t cnsl_chrdev = { - .read = cnsl_read -}; + .read = cnsl_read}; diff --git a/drivers/console.h b/drivers/console.h index a51fe67..0dc5b04 100644 --- a/drivers/console.h +++ b/drivers/console.h @@ -9,7 +9,7 @@ #pragma once -#include +#include #define CNSL_QUEUE_SIZE 1024 diff --git a/drivers/ide.c b/drivers/ide.c index f7a81a8..d5169bf 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -33,9 +33,8 @@ typedef struct _ide_drv unsigned int read_mode; - - u64_t ext_lba_base; - part_t part[MAX_SUPPORT_PARTITION_CNT]; + u64_t ext_lba_base; + part_t part[MAX_SUPPORT_PARTITION_CNT]; } ide_drive_t; typedef struct prd @@ -46,12 +45,13 @@ typedef struct prd unsigned int eot : 1; } prd_t; -typedef struct { - u64_t lba; - u32_t scnt; - u32_t read_scnt; - char *buf; - bool finish; +typedef struct +{ + u64_t lba; + u32_t scnt; + u32_t read_scnt; + char *buf; + bool finish; wait_queue_head_t wait; } ide_request_t; @@ -75,7 +75,7 @@ unsigned int HD_CHL1_CTL_BASE = 0x376; void ide_printl() { - printl(MPL_IDE, "ide pio cnt %d dma cnt %d irq cnt %d", drv.pio_cnt, drv.dma_cnt, drv.irq_cnt); + printl(MPL_IDE, "ide pio cnt %d dma cnt %d irq cnt %d", drv.pio_cnt, drv.dma_cnt, drv.irq_cnt); } void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) @@ -85,20 +85,20 @@ void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) ide_printl(); - outb(0x00, REG_CTL(dev)); - outb(0x40|0x00, REG_DEVSEL(dev)); + outb(0x00, REG_CTL(dev)); + outb(0x40 | 0x00, REG_DEVSEL(dev)); - outb((u8)((sect_cnt>>8)&0xFF), REG_NSECTOR(dev)); // High - outb((u8)((sect_nr>>24)&0xFF), REG_LBAL(dev)); - outb((u8)((sect_nr>>32)&0xFF), REG_LBAM(dev)); - outb((u8)((sect_nr>>40)&0xFF), REG_LBAH(dev)); + outb((u8)((sect_cnt >> 8) & 0xFF), REG_NSECTOR(dev)); // High + outb((u8)((sect_nr >> 24) & 0xFF), REG_LBAL(dev)); + outb((u8)((sect_nr >> 32) & 0xFF), REG_LBAM(dev)); + outb((u8)((sect_nr >> 40) & 0xFF), REG_LBAH(dev)); - outb((u8)((sect_cnt>>0)&0xFF), REG_NSECTOR(dev)); // Low - outb((u8)((sect_nr>> 0)&0xFF), REG_LBAL(dev)); - outb((u8)((sect_nr>> 8)&0xFF), REG_LBAM(dev)); - outb((u8)((sect_nr>>16)&0xFF), REG_LBAH(dev)); + outb((u8)((sect_cnt >> 0) & 0xFF), REG_NSECTOR(dev)); // Low + outb((u8)((sect_nr >> 0) & 0xFF), REG_LBAL(dev)); + outb((u8)((sect_nr >> 8) & 0xFF), REG_LBAM(dev)); + outb((u8)((sect_nr >> 16) & 0xFF), REG_LBAH(dev)); - outb(cmd, REG_CMD(dev)); + outb(cmd, REG_CMD(dev)); } part_t *ide_get_part(dev_t dev) @@ -119,19 +119,19 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) down(&mutex); r->lba = lba; - r->scnt= scnt; - r->read_scnt= 0; + r->scnt = scnt; + r->read_scnt = 0; r->buf = buf; r->finish = false; init_wait_queue(&r->wait); - task_union * task = current; + task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&r->wait, &wait); ide_cmd_out(0, scnt, lba, HD_CMD_READ_EXT); - - while(true) + + while (true) { //printd("%s pid %d is going to wait\n", __func__, sysc_getpid()); task->state = TASK_WAIT; @@ -140,7 +140,7 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) //printd("%s pid %d finish %u read_scnt %u scnt %u\n", __func__, sysc_getpid(), r->finish, r->read_scnt, r->scnt); irq_restore(flags); - if(finish) + if (finish) break; schedule(); @@ -152,7 +152,6 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) del_wait_queue(&r->wait, &wait); } - unsigned int sys_clock(); void ide_pci_init(pci_device_t *pci) @@ -168,15 +167,14 @@ void ide_pci_init(pci_device_t *pci) unsigned int iobase = pci_read_config_long(pci_cmd(pci, PCI_BAR4)); printk(" ide pci Base IO Address Register %08x\n", iobase); iobase &= 0xFFFC; - drv.iobase = iobase; - drv.bus_cmd = iobase + PCI_IDE_CMD; - drv.bus_status = iobase + PCI_IDE_STATUS; - drv.bus_prdt = iobase + PCI_IDE_PRDT; - + drv.iobase = iobase; + drv.bus_cmd = iobase + PCI_IDE_CMD; + drv.bus_status = iobase + PCI_IDE_STATUS; + drv.bus_prdt = iobase + PCI_IDE_PRDT; int i; printk(" BARS: "); - for(i=0; i<6; ++i) + for (i = 0; i < 6; ++i) { printk("%08x ", pci->bars[i]); pci->bars[i] &= (~1UL); @@ -193,7 +191,6 @@ void ide_pci_init(pci_device_t *pci) //printl(18, "channel0: cmd %04x ctl %04x channel1: cmd %04x ctl %04x", HD_CHL0_CMD_BASE, HD_CHL0_CTL_BASE, HD_CHL1_CMD_BASE, HD_CHL1_CTL_BASE); } - void ide_status() { u8_t idest = inb(REG_STATUS(0)); @@ -204,23 +201,22 @@ void ide_status() void ide_debug() { unsigned int nsect = 1; - char *buf = kmalloc(1*SECT_SIZE, 0); - if(buf == 0) + char *buf = kmalloc(1 * SECT_SIZE, 0); + if (buf == 0) panic("out of memory"); ide_do_read(0, nsect, buf); - u16_t sig = *((u16_t *) (buf+510)); + u16_t sig = *((u16_t *)(buf + 510)); printk("%s SIG: %04x\n", __func__, sig); kfree(buf); } - void init_pci_controller(unsigned int classcode) { pci_device_t *pci = pci_find_device_by_classcode(classcode); - if(pci != 0 && pci->intr_line < 16) + if (pci != 0 && pci->intr_line < 16) { printk("found pci vendor %04x device %04x class %04x intr %d\n", pci->vendor, pci->device, pci->classcode, pci->intr_line); //printl(17, "found pci vendor %04x device %04x class %04x intr %d", pci->vendor, pci->device, pci->classcode, pci->intr_line); @@ -229,7 +225,6 @@ void init_pci_controller(unsigned int classcode) } } - void ide_default_intr() { //printd("%s\n", __func__); @@ -238,26 +233,26 @@ void ide_default_intr() drv.irq_cnt++; status = inb(drv.bus_status); - if(0 == (status & PCI_IDE_STATUS_INTR)) + if (0 == (status & PCI_IDE_STATUS_INTR)) { - return ; + return; } status |= PCI_IDE_STATUS_INTR; outb(status, drv.bus_status); - outb(0x00, drv.bus_cmd); + outb(0x00, drv.bus_cmd); u16_t sig = 0; - if(drv.read_mode == HD_CMD_READ_EXT) + if (drv.read_mode == HD_CMD_READ_EXT) { - insl(REG_DATA(0), ide_request.buf + ide_request.read_scnt*(SECT_SIZE), (SECT_SIZE)>>2); + insl(REG_DATA(0), ide_request.buf + ide_request.read_scnt * (SECT_SIZE), (SECT_SIZE) >> 2); ide_request.read_scnt++; - sig = *((u16_t *) (ide_request.buf+510)); + sig = *((u16_t *)(ide_request.buf + 510)); } - if(drv.read_mode == HD_CMD_READ_DMA) + if (drv.read_mode == HD_CMD_READ_DMA) { - sig = *((u16_t *) (dma_data+510)); + sig = *((u16_t *)(dma_data + 510)); } ide_printl(); @@ -268,47 +263,47 @@ void ide_default_intr() outb(PCI_IDE_CMD_STOP, drv.bus_cmd); wake_up(&ide_request.wait); - if(drv.read_mode == HD_CMD_READ_EXT) + if (drv.read_mode == HD_CMD_READ_EXT) { - if(ide_request.read_scnt == ide_request.scnt) + if (ide_request.read_scnt == ide_request.scnt) ide_request.finish = true; } up(&mutex); } - void ide_irq() { ide_intr_func(); } -prd_t prd __attribute__((aligned(64*1024))); +prd_t prd __attribute__((aligned(64 * 1024))); unsigned long gprdt = 0; -#define DELAY400NS { \ - inb(HD_CHL0_CTL_BASE); \ - inb(HD_CHL0_CTL_BASE); \ - inb(HD_CHL0_CTL_BASE); \ - inb(HD_CHL0_CTL_BASE); \ -} +#define DELAY400NS \ + { \ + inb(HD_CHL0_CTL_BASE); \ + inb(HD_CHL0_CTL_BASE); \ + inb(HD_CHL0_CTL_BASE); \ + inb(HD_CHL0_CTL_BASE); \ + } void ide_dma_pci_lba48() { - drv.dma_cnt ++; + drv.dma_cnt++; drv.read_mode = HD_CMD_READ_DMA; #if 1 memset((void *)&prd, 0, sizeof(prd)); unsigned long addr = alloc_one_page(0); - dma_data = (char *) addr; + dma_data = (char *)addr; memset(dma_data, 0xBB, 512); prd.addr = va2pa(addr); - prd.cnt = 512; - prd.eot = 1; + prd.cnt = 512; + prd.eot = 1; gprdt = va2pa(&prd); printl(16, "gprdt %08x &prdt %08x prd.addr %08x addr %08x", - gprdt, &prd, prd.addr, addr); + gprdt, &prd, prd.addr, addr); outb(PCI_IDE_CMD_STOP, drv.bus_cmd); unsigned short status = inb(drv.bus_status); @@ -342,84 +337,84 @@ void ide_dma_pci_lba48() } #endif - outb(0x00, HD_CHL0_CTL_BASE); // Device Control + outb(0x00, HD_CHL0_CTL_BASE); // Device Control - outb(0x00, HD_CHL0_CMD_BASE+HD_FEATURES); - outb(0x00, HD_CHL0_CMD_BASE+HD_NSECTOR); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAL); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAM); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAH); + outb(0x00, HD_CHL0_CMD_BASE + HD_FEATURES); + outb(0x00, HD_CHL0_CMD_BASE + HD_NSECTOR); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAL); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAM); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAH); - outb(0x00, HD_CHL0_CMD_BASE+HD_FEATURES); - outb(0x01, HD_CHL0_CMD_BASE+HD_NSECTOR); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAL); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAM); - outb(0x00, HD_CHL0_CMD_BASE+HD_LBAH); + outb(0x00, HD_CHL0_CMD_BASE + HD_FEATURES); + outb(0x01, HD_CHL0_CMD_BASE + HD_NSECTOR); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAL); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAM); + outb(0x00, HD_CHL0_CMD_BASE + HD_LBAH); - outb(0x40, HD_CHL0_CMD_BASE+HD_DEVSEL); + outb(0x40, HD_CHL0_CMD_BASE + HD_DEVSEL); - outb(HD_CMD_READ_DMA, HD_CHL0_CMD_BASE+HD_CMD); + outb(HD_CMD_READ_DMA, HD_CHL0_CMD_BASE + HD_CMD); inb(drv.bus_cmd); inb(drv.bus_status); unsigned short w = inb(drv.bus_cmd); - outb(w|PCI_IDE_CMD_WRITE|PCI_IDE_CMD_START, drv.bus_cmd); + outb(w | PCI_IDE_CMD_WRITE | PCI_IDE_CMD_START, drv.bus_cmd); inb(drv.bus_cmd); inb(drv.bus_status); } -typedef struct { +typedef struct +{ u8_t a; u8_t b; - u16_t lbah; // lba high + u16_t lbah; // lba high u8_t type; u8_t f; u16_t scnth; // sector count high u32_t lba; // lba low u32_t scnt; // sector count -} hd_part_t ; - +} hd_part_t; void ide_read_extended_partition(u64_t lba, unsigned int inx) { - if(inx >= MAX_SUPPORT_PARTITION_CNT) - return ; + if (inx >= MAX_SUPPORT_PARTITION_CNT) + return; unsigned int i; char *buf = kmalloc(512, 0); - if(buf == 0) + if (buf == 0) panic("no memory"); ide_do_read(lba, 1, buf); - u16_t sig = *((u16_t *) (buf+510)); - if(sig != 0xAA55) + u16_t sig = *((u16_t *)(buf + 510)); + if (sig != 0xAA55) panic("bad partition sect"); - hd_part_t *p = (hd_part_t *)(buf+PARTITION_TABLE_OFFSET); + hd_part_t *p = (hd_part_t *)(buf + PARTITION_TABLE_OFFSET); printd("%s:%d lba %d \n", __func__, __LINE__, lba); - for(i=0; itype == 0) + if (p->type == 0) continue; //u64_t part_lba = lba + (p->lba|((p->lbah*1ULL)<<32)); //u64_t part_scnt= p->scnt | ((p->scnth*1ULL)<<32); - u64_t part_lba = lba + p->lba; - u64_t part_scnt = p->scnt; + u64_t part_lba = lba + p->lba; + u64_t part_scnt = p->scnt; - if(p->type != 0x05) + if (p->type != 0x05) { - drv.part[inx].lba_start = part_lba; - drv.part[inx].lba_end = part_lba+part_scnt; + drv.part[inx].lba_start = part_lba; + drv.part[inx].lba_end = part_lba + part_scnt; printk(" logic partition[%02d] [%02x] LBA base %10d end %10d\n", inx, p->type, (unsigned int)(drv.part[inx].lba_start), (unsigned int)(drv.part[inx].lba_end - 1)); } else { part_lba = drv.ext_lba_base + p->lba; - printk(" extended [%02x] LBA base %10d end %10d\n", p->type, (unsigned int)(part_lba), (unsigned int)(part_lba+part_scnt - 1)); - ide_read_extended_partition(part_lba, inx+1); + printk(" extended [%02x] LBA base %10d end %10d\n", p->type, (unsigned int)(part_lba), (unsigned int)(part_lba + part_scnt - 1)); + ide_read_extended_partition(part_lba, inx + 1); } } @@ -431,48 +426,47 @@ void ide_read_partition() printk("reading partitions....\n"); unsigned int i; char *buf = kmalloc(512, 0); - if(buf == 0) + if (buf == 0) panic("no memory"); ide_do_read(0, 1, buf); - u16_t sig = *((u16_t *) (buf+510)); - if(sig != 0xAA55) + u16_t sig = *((u16_t *)(buf + 510)); + if (sig != 0xAA55) panic("bad partition sect"); - hd_part_t *p = (hd_part_t *)(buf+PARTITION_TABLE_OFFSET); + hd_part_t *p = (hd_part_t *)(buf + PARTITION_TABLE_OFFSET); unsigned int ext_inx = ~0U; - for(i=0; itype == 0) + if (p->type == 0) continue; //u64_t part_lba = p->lba|((p->lbah*1ULL)<<32); //u64_t part_scnt= p->scnt | ((p->scnth*1ULL)<<32); - u64_t part_lba = p->lba; - u64_t part_scnt = p->scnt; - - drv.part[i].lba_start = part_lba; - drv.part[i].lba_end = part_lba+part_scnt; + u64_t part_lba = p->lba; + u64_t part_scnt = p->scnt; + drv.part[i].lba_start = part_lba; + drv.part[i].lba_end = part_lba + part_scnt; - if(p->type == 0x05) + if (p->type == 0x05) { - if(drv.ext_lba_base == 0) + if (drv.ext_lba_base == 0) { - drv.ext_lba_base = drv.part[i].lba_start; + drv.ext_lba_base = drv.part[i].lba_start; ext_inx = i; } } - printk("primary partition[%02d] [%02x] LBA base %10d end %10d\n", i, p->type, (unsigned int)(part_lba), (unsigned int)(part_lba+part_scnt - 1)); + printk("primary partition[%02d] [%02x] LBA base %10d end %10d\n", i, p->type, (unsigned int)(part_lba), (unsigned int)(part_lba + part_scnt - 1)); } kfree(buf); - if(ext_inx != ~0U) + if (ext_inx != ~0U) ide_read_extended_partition(drv.part[ext_inx].lba_start, 4); } diff --git a/drivers/ide.h b/drivers/ide.h index fecae71..87a6b4b 100644 --- a/drivers/ide.h +++ b/drivers/ide.h @@ -16,71 +16,70 @@ extern unsigned int HD_CHL1_CMD_BASE; extern unsigned int HD_CHL0_CTL_BASE; extern unsigned int HD_CHL1_CTL_BASE; -#define HD_DATA 0 -#define HD_FEATURES 1 -#define HD_ERR 1 -#define HD_ERR_BB 0x80 -#define HD_ERR_ECC 0x40 -#define HD_ERR_ID 0x10 -#define HD_ERR_AC 0x04 -#define HD_ERR_TK 0x02 -#define HD_ERR_DM 0x01 -#define HD_NSECTOR 2 -#define HD_LBAL 3 -#define HD_LBAM 4 -#define HD_LBAH 5 -#define HD_DEVSEL 6 -#define HD_CMD 7 -#define HD_STATUS 7 /* controller status */ -#define HD_STATUS_BSY 0x80 /* controller busy */ -#define HD_STATUS_RDY 0x40 /* drive ready */ -#define HD_STATUS_WF 0x20 /* write fault */ -#define HD_STATUS_SEEK_CMPT 0x10 /* seek complete */ -#define HD_STATUS_DRQ 0x08 /* data transfer request */ -#define HD_STATUS_CRD 0x04 /* correct data */ -#define HD_STATUS_IDX 0x02 /* index pulse */ -#define HD_STATUS_ERR 0x01 /* error */ -#define HD_CMD_IDLE 0x00 -#define HD_CMD_RECALIBRATE 0x10 -#define HD_CMD_READ 0x20 /* read data */ -#define HD_CMD_READ_EXT 0x24 /* read data (LBA-48 bit)*/ -#define HD_CMD_READ_DMA 0x25 /* read data DMA LBA48 */ -#define HD_CMD_WRITE 0x30 -#define HD_CMD_WRITE_EXT 0x34 -#define HD_CMD_READ_VERIFY 0x40 -#define HD_CMD_FORMAT 0x50 -#define HD_CMD_SEEK 0x70 -#define HD_CMD_DIAG 0x90 -#define HD_CMD_SPECIFY 0x91 -#define HD_CMD_IDENTIFY 0xEC - -#define HD_CTL 0 -#define HD_CTL_HOB 0x80 /* high order byte (LBA-48bit) */ +#define HD_DATA 0 +#define HD_FEATURES 1 +#define HD_ERR 1 +#define HD_ERR_BB 0x80 +#define HD_ERR_ECC 0x40 +#define HD_ERR_ID 0x10 +#define HD_ERR_AC 0x04 +#define HD_ERR_TK 0x02 +#define HD_ERR_DM 0x01 +#define HD_NSECTOR 2 +#define HD_LBAL 3 +#define HD_LBAM 4 +#define HD_LBAH 5 +#define HD_DEVSEL 6 +#define HD_CMD 7 +#define HD_STATUS 7 /* controller status */ +#define HD_STATUS_BSY 0x80 /* controller busy */ +#define HD_STATUS_RDY 0x40 /* drive ready */ +#define HD_STATUS_WF 0x20 /* write fault */ +#define HD_STATUS_SEEK_CMPT 0x10 /* seek complete */ +#define HD_STATUS_DRQ 0x08 /* data transfer request */ +#define HD_STATUS_CRD 0x04 /* correct data */ +#define HD_STATUS_IDX 0x02 /* index pulse */ +#define HD_STATUS_ERR 0x01 /* error */ +#define HD_CMD_IDLE 0x00 +#define HD_CMD_RECALIBRATE 0x10 +#define HD_CMD_READ 0x20 /* read data */ +#define HD_CMD_READ_EXT 0x24 /* read data (LBA-48 bit)*/ +#define HD_CMD_READ_DMA 0x25 /* read data DMA LBA48 */ +#define HD_CMD_WRITE 0x30 +#define HD_CMD_WRITE_EXT 0x34 +#define HD_CMD_READ_VERIFY 0x40 +#define HD_CMD_FORMAT 0x50 +#define HD_CMD_SEEK 0x70 +#define HD_CMD_DIAG 0x90 +#define HD_CMD_SPECIFY 0x91 +#define HD_CMD_IDENTIFY 0xEC + +#define HD_CTL 0 +#define HD_CTL_HOB 0x80 /* high order byte (LBA-48bit) */ //#define HD_CTL_NOECC 0x40 /* disable ecc retry */ //#define HD_CTL_EIGHTHEADS 0x08 /* more than 8 heads */ -#define HD_CTL_SRST 0x04 /* soft reset controller */ -#define HD_CTL_NIEN 0x02 /* disable interrupts */ +#define HD_CTL_SRST 0x04 /* soft reset controller */ +#define HD_CTL_NIEN 0x02 /* disable interrupts */ -#define HD_GET_CHL(dev) (0) /* only support channel 0 */ -#define HD_GET_DEV(dev) (0) /* only support one hard disk */ +#define HD_GET_CHL(dev) (0) /* only support channel 0 */ +#define HD_GET_DEV(dev) (0) /* only support one hard disk */ -#define REG_CMD_BASE(dev, offset) ( HD_GET_CHL(dev) ? (HD_CHL1_CMD_BASE+offset) : (HD_CHL0_CMD_BASE+offset) ) -#define REG_CTL_BASE(dev, offset) ( HD_GET_CHL(dev) ? (HD_CHL1_CTL_BASE+offset) : (HD_CHL0_CTL_BASE+offset) ) +#define REG_CMD_BASE(dev, offset) (HD_GET_CHL(dev) ? (HD_CHL1_CMD_BASE + offset) : (HD_CHL0_CMD_BASE + offset)) +#define REG_CTL_BASE(dev, offset) (HD_GET_CHL(dev) ? (HD_CHL1_CTL_BASE + offset) : (HD_CHL0_CTL_BASE + offset)) -#define REG_DATA(dev) REG_CMD_BASE(dev, HD_DATA) -#define REG_ERR(dev) REG_CMD_BASE(dev, HD_ERR) -#define REG_NSECTOR(dev) REG_CMD_BASE(dev, HD_NSECTOR) -#define REG_LBAL(dev) REG_CMD_BASE(dev, HD_LBAL) -#define REG_LBAM(dev) REG_CMD_BASE(dev, HD_LBAM) -#define REG_LBAH(dev) REG_CMD_BASE(dev, HD_LBAH) -#define REG_DEVSEL(dev) REG_CMD_BASE(dev, HD_DEVSEL) -#define REG_STATUS(dev) REG_CMD_BASE(dev, HD_STATUS) -#define REG_FEATURES(dev) REG_CMD_BASE(dev, HD_FEATURES) -#define REG_CMD(dev) REG_CMD_BASE(dev, HD_CMD) -#define REG_CTL(dev) REG_CTL_BASE(dev, HD_CTL) - -#define SECT_SIZE 512 +#define REG_DATA(dev) REG_CMD_BASE(dev, HD_DATA) +#define REG_ERR(dev) REG_CMD_BASE(dev, HD_ERR) +#define REG_NSECTOR(dev) REG_CMD_BASE(dev, HD_NSECTOR) +#define REG_LBAL(dev) REG_CMD_BASE(dev, HD_LBAL) +#define REG_LBAM(dev) REG_CMD_BASE(dev, HD_LBAM) +#define REG_LBAH(dev) REG_CMD_BASE(dev, HD_LBAH) +#define REG_DEVSEL(dev) REG_CMD_BASE(dev, HD_DEVSEL) +#define REG_STATUS(dev) REG_CMD_BASE(dev, HD_STATUS) +#define REG_FEATURES(dev) REG_CMD_BASE(dev, HD_FEATURES) +#define REG_CMD(dev) REG_CMD_BASE(dev, HD_CMD) +#define REG_CTL(dev) REG_CTL_BASE(dev, HD_CTL) +#define SECT_SIZE 512 #define hd_rd_data(dev, buf, count) hd_rd_port(REG_DATA(dev), buf, count) @@ -89,44 +88,40 @@ extern unsigned int HD_CHL1_CTL_BASE; #define hd_drq(dev) ((inb(REG_STATUS(dev)) & HD_STATUS_DRQ)) #define hd_err(dev) ((inb(REG_STATUS(dev)) & HD_STATUS_ERR)) - - -#define ATA_IDENT_DEVTYPE 0 -#define ATA_IDENT_CYLINDERS 2 -#define ATA_IDENT_HEADS 6 -#define ATA_IDENT_SECTORS 12 -#define ATA_IDENT_SERIAL 20 -#define ATA_IDENT_MODEL 54 -#define ATA_IDENT_CAPABILITIES 98 -#define ATA_IDENT_FIELDVALID 106 -#define ATA_IDENT_MAX_LBA 120 -#define ATA_IDENT_COMMANDSETS 164 -#define ATA_IDENT_MAX_LBA_EXT 200 - - - -#define PCI_IDE_CMD 0 - #define PCI_IDE_CMD_STOP 0x00 - #define PCI_IDE_CMD_READ 0x00 - #define PCI_IDE_CMD_START 0x01 - #define PCI_IDE_CMD_WRITE 0x08 -#define PCI_IDE_STATUS 2 - #define PCI_IDE_STATUS_ACT 0x01 - #define PCI_IDE_STATUS_ERR 0x02 - #define PCI_IDE_STATUS_INTR 0x04 - #define PCI_IDE_STATUS_DRV0 0x20 - #define PCI_IDE_STATUS_DRV1 0x40 - #define PCI_IDE_STATUS_SIMPLEX 0x80 -#define PCI_IDE_PRDT 4 - - -#define PARTITION_CNT 4 -#define PARTITION_TABLE_OFFSET 0x1BE -#define MAX_SUPPORT_PARTITION_CNT 16 - -typedef struct { - u64_t lba_start; - u64_t lba_end; +#define ATA_IDENT_DEVTYPE 0 +#define ATA_IDENT_CYLINDERS 2 +#define ATA_IDENT_HEADS 6 +#define ATA_IDENT_SECTORS 12 +#define ATA_IDENT_SERIAL 20 +#define ATA_IDENT_MODEL 54 +#define ATA_IDENT_CAPABILITIES 98 +#define ATA_IDENT_FIELDVALID 106 +#define ATA_IDENT_MAX_LBA 120 +#define ATA_IDENT_COMMANDSETS 164 +#define ATA_IDENT_MAX_LBA_EXT 200 + +#define PCI_IDE_CMD 0 +#define PCI_IDE_CMD_STOP 0x00 +#define PCI_IDE_CMD_READ 0x00 +#define PCI_IDE_CMD_START 0x01 +#define PCI_IDE_CMD_WRITE 0x08 +#define PCI_IDE_STATUS 2 +#define PCI_IDE_STATUS_ACT 0x01 +#define PCI_IDE_STATUS_ERR 0x02 +#define PCI_IDE_STATUS_INTR 0x04 +#define PCI_IDE_STATUS_DRV0 0x20 +#define PCI_IDE_STATUS_DRV1 0x40 +#define PCI_IDE_STATUS_SIMPLEX 0x80 +#define PCI_IDE_PRDT 4 + +#define PARTITION_CNT 4 +#define PARTITION_TABLE_OFFSET 0x1BE +#define MAX_SUPPORT_PARTITION_CNT 16 + +typedef struct +{ + u64_t lba_start; + u64_t lba_end; } part_t; void ide_do_read(u64_t lba, u32_t scnt, char *buf); diff --git a/drivers/keyboard.c b/drivers/keyboard.c index aa70b45..3af4e81 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -29,29 +29,378 @@ void vga_dbg_toggle(); int debug_wait_queue_put(unsigned int v); void ide_dma_pci_lba48(); - void kbd_debug(unsigned char scan_code); -char kbd_char_tbl[]={0,0, -'1','2','3','4','5','6','7','8','9','0','-','=','\b',0, -'q','w','e','r','t','y','u','i','o','p','[',']','\n',0, -'a','s','d','f','g','h','j','k','l',';','\'','`',0,'\\', -'z','x','c','v','b','n','m',',','.','/',0,0,0,' ', -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +char kbd_char_tbl[] = { + 0, + 0, + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + '0', + '-', + '=', + '\b', + 0, + 'q', + 'w', + 'e', + 'r', + 't', + 'y', + 'u', + 'i', + 'o', + 'p', + '[', + ']', + '\n', + 0, + 'a', + 's', + 'd', + 'f', + 'g', + 'h', + 'j', + 'k', + 'l', + ';', + '\'', + '`', + 0, + '\\', + 'z', + 'x', + 'c', + 'v', + 'b', + 'n', + 'm', + ',', + '.', + '/', + 0, + 0, + 0, + ' ', + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, }; - -void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) +void kbd_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { unsigned char scan_code; @@ -59,9 +408,9 @@ void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) kbd_debug(scan_code); - if(0x80 & scan_code) // break code + if (0x80 & scan_code) // break code { - return ; + return; } unsigned int inx = scan_code & 0xFF; @@ -74,42 +423,41 @@ void kbd_debug(unsigned char scan_code) static unsigned long kbd_cnt = 0; printl(MPL_KEYBOARD, "keyboard irq: %d scan code %02x", kbd_cnt++, scan_code); - if(scan_code == 0x01) // Esc + if (scan_code == 0x01) // Esc reboot(); printd("[%02x]", scan_code); - - if(scan_code == 0x3B) // F1 + if (scan_code == 0x3B) // F1 vga_switch(0); - if(scan_code == 0x3C) // F2 + if (scan_code == 0x3C) // F2 vga_switch(1); - if(scan_code == 0x3D) // F3 + if (scan_code == 0x3D) // F3 vga_switch(2); - if(scan_code == 0x3E) // F4 + if (scan_code == 0x3E) // F4 vga_switch(3); - if(scan_code == 0x3F) // F5 + if (scan_code == 0x3F) // F5 debug_wait_queue_put(0); - if(scan_code == 0x40) // F6 + if (scan_code == 0x40) // F6 debug_wait_queue_put(1); - if(scan_code == 0x41) // F7 + if (scan_code == 0x41) // F7 debug_wait_queue_put(2); - if(scan_code == 0x42) // F8 + if (scan_code == 0x42) // F8 debug_wait_queue_put(7); - if(scan_code == 0x43) // F9 + if (scan_code == 0x43) // F9 ide_dma_pci_lba48(); - if(scan_code == 0x44) // F10 + if (scan_code == 0x44) // F10 ide_debug(); - if(scan_code == 0x57) // F11 + if (scan_code == 0x57) // F11 { asm("cli;"); - while(1); + while (1) + ; } - if(scan_code == 0x58) // F12 + if (scan_code == 0x58) // F12 vga_dbg_toggle(); - //ide_status(); } diff --git a/drivers/vga.c b/drivers/vga.c index a7724e7..e14369c 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -13,43 +13,46 @@ #include #include -typedef struct { - u8_t c; - u8_t f; +typedef struct +{ + u8_t c; + u8_t f; } __attribute__((packed)) vga_char_t; -#define VIDEO_ADDR 0xC00B8000 +#define VIDEO_ADDR 0xC00B8000 -#define VGA_CRTC_ADDR 0x3D4 -#define VGA_CRTC_DATA 0x3D5 -#define VGA_CRTC_START_ADDR_H 0xC -#define VGA_CRTC_START_ADDR_L 0xD -#define VGA_CRTC_CURSOR_H 0xE -#define VGA_CRTC_CURSOR_L 0xF +#define VGA_CRTC_ADDR 0x3D4 +#define VGA_CRTC_DATA 0x3D5 +#define VGA_CRTC_START_ADDR_H 0xC +#define VGA_CRTC_START_ADDR_L 0xD +#define VGA_CRTC_CURSOR_H 0xE +#define VGA_CRTC_CURSOR_L 0xF -#define LINES_PER_SCREEN 25 -#define CHARS_PER_LINE 80 -#define BYTES_PER_LINE (sizeof(vga_char_t)*CHARS_PER_LINE) -#define MAX_LINES_PER_SCREEN 32 +#define LINES_PER_SCREEN 25 +#define CHARS_PER_LINE 80 +#define BYTES_PER_LINE (sizeof(vga_char_t) * CHARS_PER_LINE) +#define MAX_LINES_PER_SCREEN 32 -#define TAB_ALIGN 4 -#define TAB_MASK (TAB_ALIGN-1) +#define TAB_ALIGN 4 +#define TAB_MASK (TAB_ALIGN - 1) -typedef struct { +typedef struct +{ unsigned int id; vga_char_t *base; unsigned int offset; } vga_screen_t; -#define VGA_SCREEN_CNT 3 -#define VGA_MAX_SCREEN_CNT ((VGA_SCREEN_CNT) + 1) -unsigned int vga_screen_cnt() {return VGA_SCREEN_CNT;} +#define VGA_SCREEN_CNT 3 +#define VGA_MAX_SCREEN_CNT ((VGA_SCREEN_CNT) + 1) +unsigned int vga_screen_cnt() +{ + return VGA_SCREEN_CNT; +} vga_screen_t vga_screen[VGA_MAX_SCREEN_CNT] = { - { - 0, - (vga_char_t *)VIDEO_ADDR, - 0 - }, + {0, + (vga_char_t *)VIDEO_ADDR, + 0}, }; static vga_screen_t *vga_current_screen = vga_screen + 0; @@ -80,53 +83,51 @@ vga_char_t vga_char(unsigned char c, unsigned char f) void vga_set_cursor_pos(vga_screen_t *s) { - unsigned int base = s->id*MAX_LINES_PER_SCREEN*CHARS_PER_LINE; + unsigned int base = s->id * MAX_LINES_PER_SCREEN * CHARS_PER_LINE; unsigned int offset = base + s->offset; - base = s->id*MAX_LINES_PER_SCREEN*CHARS_PER_LINE; + base = s->id * MAX_LINES_PER_SCREEN * CHARS_PER_LINE; offset = base + s->offset; unsigned long flags; irq_save(flags); - outb(VGA_CRTC_CURSOR_H, VGA_CRTC_ADDR); - outb((offset>>8) & 0xFF, VGA_CRTC_DATA); - outb(VGA_CRTC_CURSOR_L, VGA_CRTC_ADDR); - outb(offset & 0xFF, VGA_CRTC_DATA); + outb(VGA_CRTC_CURSOR_H, VGA_CRTC_ADDR); + outb((offset >> 8) & 0xFF, VGA_CRTC_DATA); + outb(VGA_CRTC_CURSOR_L, VGA_CRTC_ADDR); + outb(offset & 0xFF, VGA_CRTC_DATA); irq_restore(flags); } void vga_clear(vga_screen_t *s, unsigned int b, unsigned int e) { - if(e <= b) - return ; + if (e <= b) + return; vga_char_t *base = s->base; base += b; - memset((void *)base, 0, (e-b)*sizeof(vga_char_t)); + memset((void *)base, 0, (e - b) * sizeof(vga_char_t)); } void vga_scroll_up(vga_screen_t *s) { int delta = ypos(s) + 1 - LINES_PER_SCREEN; - if(delta <= 0) + if (delta <= 0) return; vga_char_t *base = s->base; - vga_char_t *head = base + delta*CHARS_PER_LINE; - vga_char_t *empt = base + (LINES_PER_SCREEN-delta)*CHARS_PER_LINE; + vga_char_t *head = base + delta * CHARS_PER_LINE; + vga_char_t *empt = base + (LINES_PER_SCREEN - delta) * CHARS_PER_LINE; - memcpy((void *)base, (void *)head, (empt-base)*sizeof(vga_char_t)); + memcpy((void *)base, (void *)head, (empt - base) * sizeof(vga_char_t)); //memset((void *)empt, 0, delta*BYTES_PER_LINE); - vga_clear(s, (LINES_PER_SCREEN-delta)*CHARS_PER_LINE, LINES_PER_SCREEN*CHARS_PER_LINE); + vga_clear(s, (LINES_PER_SCREEN - delta) * CHARS_PER_LINE, LINES_PER_SCREEN * CHARS_PER_LINE); set_offset(s, xpos(s), ypos(s) - delta); } - - void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) { vga_screen_t *s = vga_screen + nr; @@ -136,8 +137,8 @@ void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) bool need_clear = true; bool need_forward = true; unsigned int old_offset = s->offset; - - switch(c) + + switch (c) { case '\r': set_offset(s, 0, ypos(s)); @@ -156,31 +157,30 @@ void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) default: need_clear = false; pv[s->offset] = vga_char(c, color); - set_offset(s, xpos(s)+(need_forward?1 : 0), ypos(s)); + set_offset(s, xpos(s) + (need_forward ? 1 : 0), ypos(s)); break; } - if(need_clear) + if (need_clear) { vga_clear(s, old_offset, s->offset); } vga_scroll_up(s); - if(vga_current_screen == s) + if (vga_current_screen == s) vga_set_cursor_pos(s); } - void vga_puts(unsigned int nr, const char *buf, unsigned char color) { assert(buf != 0); - if(nr >= VGA_MAX_SCREEN_CNT) - return ; + if (nr >= VGA_MAX_SCREEN_CNT) + return; - char *p = (char *) buf; + char *p = (char *)buf; - while(*p) + while (*p) { vga_putc(nr, *p, color); p++; @@ -190,34 +190,34 @@ void vga_puts(unsigned int nr, const char *buf, unsigned char color) void __vga_switch(unsigned int offset) { outb(VGA_CRTC_START_ADDR_H, VGA_CRTC_ADDR); - outb((offset>>8)&0xFF, VGA_CRTC_DATA); + outb((offset >> 8) & 0xFF, VGA_CRTC_DATA); outb(VGA_CRTC_START_ADDR_L, VGA_CRTC_ADDR); - outb((offset)&0xFF, VGA_CRTC_DATA); + outb((offset)&0xFF, VGA_CRTC_DATA); } void vga_switch(unsigned int nr) { - if(nr >= VGA_MAX_SCREEN_CNT) - return ; + if (nr >= VGA_MAX_SCREEN_CNT) + return; vga_screen_t *s = vga_screen + nr; vga_current_screen = s; - unsigned int offset = 0 + (s->base - (vga_char_t*)VIDEO_ADDR); + unsigned int offset = 0 + (s->base - (vga_char_t *)VIDEO_ADDR); __vga_switch(offset); } -#define VIDEO_DBG_LINE ((VGA_MAX_SCREEN_CNT)*(MAX_LINES_PER_SCREEN)) +#define VIDEO_DBG_LINE ((VGA_MAX_SCREEN_CNT) * (MAX_LINES_PER_SCREEN)) void vga_dbg_toggle() { static bool dbg = true; unsigned int offset = 0; - if(dbg) + if (dbg) { - offset += VIDEO_DBG_LINE*CHARS_PER_LINE; + offset += VIDEO_DBG_LINE * CHARS_PER_LINE; } dbg = !dbg; @@ -231,24 +231,24 @@ void vga_dbg_clear() { int i; int line; - for(line=0; linei_size); @@ -84,24 +89,24 @@ void ext2_read_file(const ext2_inode_t *inode, char *buf) { assert(inode != 0); assert(buf != 0); - assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE); + assert(inode->i_size > 0 && inode->i_size <= MAX_SUPT_FILE_SIZE); unsigned int blkcnt = inode->i_size / EXT2_BLOCK_SIZE; - int i=0; - for(i=0; ii_block[i], 1, buf+i*EXT2_BLOCK_SIZE); + BLKRW(inode->i_block[i], 1, buf + i * EXT2_BLOCK_SIZE); printd("read block\n"); } unsigned int left = inode->i_size % EXT2_BLOCK_SIZE; - if(left) + if (left) { printd("read left %u bytes\n", left); void *blk = ext2_alloc_block(); - memcpy(buf+i*EXT2_BLOCK_SIZE, blk, left); + memcpy(buf + i * EXT2_BLOCK_SIZE, blk, left); ext2_free_block(blk); } @@ -112,19 +117,18 @@ void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, { assert(inode != 0); assert(buf != 0); - assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE); - assert(offset+size <= inode->i_size); - assert(offset % EXT2_BLOCK_SIZE == 0); // for easy - printd("offset %x size %x %x\n", offset, size, offset+size); - assert((offset+size) % EXT2_BLOCK_SIZE == 0); - - unsigned int blkid = offset / EXT2_BLOCK_SIZE; - unsigned int blkcnt = size / EXT2_BLOCK_SIZE; + assert(inode->i_size > 0 && inode->i_size <= MAX_SUPT_FILE_SIZE); + assert(offset + size <= inode->i_size); + assert(offset % EXT2_BLOCK_SIZE == 0); // for easy + printd("offset %x size %x %x\n", offset, size, offset + size); + assert((offset + size) % EXT2_BLOCK_SIZE == 0); + + unsigned int blkid = offset / EXT2_BLOCK_SIZE; + unsigned int blkcnt = size / EXT2_BLOCK_SIZE; printd("id %u cnt %u\n", blkid, blkcnt); BLKRW(inode->i_block[blkid], blkcnt, buf); } - unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsigned int *file_type) { unsigned int ino = 0; @@ -135,31 +139,31 @@ unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsi BLKRW(inode->i_block[0], 1, blk); // only support the first direct blocks - ext2_dirent_t *dirent = (ext2_dirent_t *) blk; + ext2_dirent_t *dirent = (ext2_dirent_t *)blk; char tmp[64]; - while(dirent->name_len != 0) + while (dirent->name_len != 0) { memcpy(tmp, dirent->name, dirent->name_len); tmp[dirent->name_len] = 0; printd(" dirent %s inode %u rec_len %u name_len %u type %02d\n", - tmp, dirent->inode, dirent->rec_len, dirent->name_len, dirent->file_type); + tmp, dirent->inode, dirent->rec_len, dirent->name_len, dirent->file_type); - if(strcmp(name, tmp) == 0) + if (strcmp(name, tmp) == 0) { ino = dirent->inode; *file_type = dirent->file_type; break; } - dirent = (ext2_dirent_t *) (((unsigned int)dirent) + dirent->rec_len); + dirent = (ext2_dirent_t *)(((unsigned int)dirent) + dirent->rec_len); - if(((unsigned long) dirent - (unsigned long)blk) >= EXT2_BLOCK_SIZE) + if (((unsigned long)dirent - (unsigned long)blk) >= EXT2_BLOCK_SIZE) { ino = 0; break; } } - + ext2_free_block(blk); return ino; @@ -169,10 +173,10 @@ static int get_filename_from_path(const char *path, char *file) { int i = 0; - while(*path == '/' && *path != '\0') + while (*path == '/' && *path != '\0') path++; - while(*path != '/' && *path != '\0') + while (*path != '/' && *path != '\0') file[i++] = *path++; file[i] = 0; @@ -182,7 +186,7 @@ static int get_filename_from_path(const char *path, char *file) unsigned int ext2_search_inpath(const char *path) { - if(path == 0 || strlen(path) == 0 || path[0] != '/') + if (path == 0 || strlen(path) == 0 || path[0] != '/') return 0; assert(path != 0); @@ -195,21 +199,21 @@ unsigned int ext2_search_inpath(const char *path) unsigned int ino = EXT2_ROOT_INO; - #define MAX_FILE_NAME 64 +#define MAX_FILE_NAME 64 char file[MAX_FILE_NAME]; int len; unsigned int file_type = EXT2_FT_UNKNOWN; - while((len=get_filename_from_path(path, file)) != 0) + while ((len = get_filename_from_path(path, file)) != 0) { ino = ext2_search_indir(file, inode, &file_type); - if(ino == 0) + if (ino == 0) return 0; //assert(ino != 0); path += len; - if(*path != 0) + if (*path != 0) { path++; ext2_read_inode(ino, inode); @@ -221,26 +225,25 @@ unsigned int ext2_search_inpath(const char *path) } } - if(file_type != EXT2_FT_REG_FILE) + if (file_type != EXT2_FT_REG_FILE) return 0; return ino; } - void ext2_setup_fs() { memset(&ext2_fs, 0, sizeof(ext2_fs)); char *buf = kmalloc(EXT2_BLOCK_SIZE, 0); - if(buf == 0) + if (buf == 0) panic("out of memory"); - BLKRW(1, 1, buf); // now blocksize == 1024, so blkid == 1 + BLKRW(1, 1, buf); // now blocksize == 1024, so blkid == 1 memcpy(EXT2_SB, buf, sizeof(*(EXT2_SB))); - if(EXT2_SB->s_magic != EXT2_SUPER_MAGIC) + if (EXT2_SB->s_magic != EXT2_SUPER_MAGIC) { printk("file system magic %04x\n", EXT2_SB->s_magic); panic("only support ext2 file system..."); @@ -248,37 +251,32 @@ void ext2_setup_fs() printk("Ext2 File System Information:\n"); printk(" inodes %u blocks %u free blocks %u free inodes %u\n", - EXT2_SB->s_inodes_count, EXT2_SB->s_blocks_count, EXT2_SB->s_free_blocks_count, EXT2_SB->s_free_inodes_count); + EXT2_SB->s_inodes_count, EXT2_SB->s_blocks_count, EXT2_SB->s_free_blocks_count, EXT2_SB->s_free_inodes_count); printk(" block size %u log block size %u first data block %u\n", - EXT2_BLOCK_SIZE, EXT2_SB->s_log_block_size, EXT2_SB->s_first_data_block); + EXT2_BLOCK_SIZE, EXT2_SB->s_log_block_size, EXT2_SB->s_first_data_block); printk(" blocks per group %u inodes per group %u\n", EXT2_SB->s_blocks_per_group, EXT2_SB->s_inodes_per_group); - ext2_block_cache = kmem_cache_create("ext2_block_cache", EXT2_BLOCK_SIZE, EXT2_BLOCK_SIZE); - if(0 == ext2_block_cache) + if (0 == ext2_block_cache) panic("setup ext2 block cache failed. out of memory"); ext2_inode_cache = kmem_cache_create("ext2_inode_cache", EXT2_INODE_SIZE, EXT2_INODE_SIZE); - if(0 == ext2_inode_cache) + if (0 == ext2_inode_cache) panic("setup ext2 inode cache failed. out of memory"); ext2_fs.ext2_gd = ext2_alloc_block(); assert(ext2_fs.ext2_gd != 0); - BLKRW(EXT2_SB->s_first_data_block+1, 1, (char *)ext2_fs.ext2_gd); + BLKRW(EXT2_SB->s_first_data_block + 1, 1, (char *)ext2_fs.ext2_gd); unsigned int gps = EXT2_SB->s_blocks_count / EXT2_SB->s_blocks_per_group; gps += (EXT2_SB->s_blocks_count % EXT2_SB->s_blocks_per_group) ? 1 : 0; unsigned int i; - for(i=0; ibg_inode_table, ext2_gd(i)->bg_free_blocks_count, ext2_gd(i)->bg_free_inodes_count, ext2_gd(i)->bg_used_dirs_count); + i, ext2_gd(i)->bg_inode_table, ext2_gd(i)->bg_free_blocks_count, ext2_gd(i)->bg_free_inodes_count, ext2_gd(i)->bg_used_dirs_count); } ext2_read_inode(2, &ext2_root_inode); } - - - - diff --git a/fs/fs.c b/fs/fs.c index 06879b3..78999cd 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -17,8 +17,7 @@ extern chrdev_t cnsl_chrdev; chrdev_t *chrdev[CHRDEV_SIZE] = { - &cnsl_chrdev -}; + &cnsl_chrdev}; void ext2_setup_fs(); unsigned int ext2_search_inpath(const char *path); diff --git a/fs/read.c b/fs/read.c index 7d7307b..7a3d10f 100644 --- a/fs/read.c +++ b/fs/read.c @@ -17,7 +17,7 @@ int sysc_read(int fd, void *buf, size_t count) { - if(fd<0 || fd>=NR_OPENS) + if (fd < 0 || fd >= NR_OPENS) return -EBADF; // only support char device diff --git a/fs/write.c b/fs/write.c index b80d133..729ce9d 100644 --- a/fs/write.c +++ b/fs/write.c @@ -14,14 +14,15 @@ *-------------------------------------------------------------------------- */ -#include +#include extern void vga_puts(unsigned int nr, const char *buf, unsigned char color); int sysc_write(int fd, const char *buf, unsigned long size) { - if(size < 0) return -1; + if (size < 0) + return -1; - switch(fd) + switch (fd) { case 0: vga_puts(0, buf, 0xF); diff --git a/include/assert.h b/include/assert.h index 791a6b0..fd02b99 100644 --- a/include/assert.h +++ b/include/assert.h @@ -10,14 +10,14 @@ *-------------------------------------------------------------------------- */ -#ifndef _ASSERT_H +#ifndef _ASSERT_H #define _ASSERT_H #include -#define assert(exp) \ -( (exp) \ -? (void) (0) \ -: assert_fail(__STRING(exp), __FILE__, __LINE__, __PRETTY_FUNCTION__)) +#define assert(exp) \ + ((exp) \ + ? (void)(0) \ + : assert_fail(__STRING(exp), __FILE__, __LINE__, __PRETTY_FUNCTION__)) #endif //_ASSERT_H diff --git a/include/atomic.h b/include/atomic.h index f23f7f3..63f5168 100644 --- a/include/atomic.h +++ b/include/atomic.h @@ -9,8 +9,7 @@ #pragma once -#define atomic_inc(x) __sync_add_and_fetch((x),1) -#define atomic_dec(x) __sync_sub_and_fetch((x),1) -#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) -#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) - +#define atomic_inc(x) __sync_add_and_fetch((x), 1) +#define atomic_dec(x) __sync_sub_and_fetch((x), 1) +#define atomic_add(x, y) __sync_add_and_fetch((x), (y)) +#define atomic_sub(x, y) __sync_sub_and_fetch((x), (y)) diff --git a/include/bak.ext2.h b/include/bak.ext2.h index a5474d2..2dccc34 100644 --- a/include/bak.ext2.h +++ b/include/bak.ext2.h @@ -14,47 +14,46 @@ *-------------------------------------------------------------------------- */ -#ifndef _EXT2_H +#ifndef _EXT2_H #define _EXT2_H #include -#define EXT2_BAD_INO 1 -#define EXT2_ROOT_INO 2 -#define EXT2_BOOT_LOADER_INO 5 -#define EXT2_UNDEL_DIR_INO 6 +#define EXT2_BAD_INO 1 +#define EXT2_ROOT_INO 2 +#define EXT2_BOOT_LOADER_INO 5 +#define EXT2_UNDEL_DIR_INO 6 -#define EXT2_MIN_BLOCK_SIZE 1024 -#define EXT2_MAX_BLOCK_SIZE 4096 +#define EXT2_MIN_BLOCK_SIZE 1024 +#define EXT2_MAX_BLOCK_SIZE 4096 #define EXT2_MIN_BLOCK_LOG_SIZE 10 -#define EXT2_SB (&ext2_sb) -#define EXT2_SECT (ext2_start_sect) +#define EXT2_SB (&ext2_sb) +#define EXT2_SECT (ext2_start_sect) -#ifndef EXT2_SB +#ifndef EXT2_SB #error "Please define EXT2_SB" #endif -#define EXT2_BLOCK_SIZE (EXT2_MIN_BLOCK_SIZE << \ - (EXT2_SB)->s_log_block_size) +#define EXT2_BLOCK_SIZE (EXT2_MIN_BLOCK_SIZE << (EXT2_SB)->s_log_block_size) -#define EXT2_SECT_PER_BLOCK (EXT2_BLOCK_SIZE/512) +#define EXT2_SECT_PER_BLOCK (EXT2_BLOCK_SIZE / 512) -#define EXT2_BLOCK_SIZE_BITS ((EXT2_SB)->s_log_block_size + 10) -#define EXT2_INODE_SIZE ((EXT2_SB)->s_inode_size) -#define EXT2_INODES_PER_BLOCK (EXT2_BLOCK_SIZE/EXT2_INODE_SIZE) -#define EXT2_FIRST_INO ((EXT2_SB)->s_first_ino) +#define EXT2_BLOCK_SIZE_BITS ((EXT2_SB)->s_log_block_size + 10) +#define EXT2_INODE_SIZE ((EXT2_SB)->s_inode_size) +#define EXT2_INODES_PER_BLOCK (EXT2_BLOCK_SIZE / EXT2_INODE_SIZE) +#define EXT2_FIRST_INO ((EXT2_SB)->s_first_ino) /* * 表示第一个块号. 因为SuperBlock总是从第三个扇区开始的所以如果块的大小 * 是1024的话SuperBlock的块号是1.而如果块的大小是2048或4096则SuperBlock * 的块号是0 */ -#define EXT2_FIRST_BLOCK_ID (EXT2_BLOCK_SIZE == 1024) +#define EXT2_FIRST_BLOCK_ID (EXT2_BLOCK_SIZE == 1024) -#define EXT2_BLOCKS_PER_GROUP ((EXT2_SB)->s_blocks_per_group) -#define EXT2_DESC_PER_BLOCK ((EXT2_SB)->s_desc_per_block) -#define EXT2_INODES_PER_GROUP ((EXT2_SB)->s_inodes_per_group) -#define EXT2_INODES_COUNT ((EXT2_SB)->s_inodes_count) +#define EXT2_BLOCKS_PER_GROUP ((EXT2_SB)->s_blocks_per_group) +#define EXT2_DESC_PER_BLOCK ((EXT2_SB)->s_desc_per_block) +#define EXT2_INODES_PER_GROUP ((EXT2_SB)->s_inodes_per_group) +#define EXT2_INODES_COUNT ((EXT2_SB)->s_inodes_count) /* * ------------------------------------------------------------------------ @@ -63,7 +62,7 @@ */ typedef struct ext2_superblock { -/* + /* u32 s_inodes_count; u32 s_blocks_count; u32 s_r_blocks_count; @@ -79,31 +78,31 @@ typedef struct ext2_superblock // So Much Items // I do not want to write down ... */ - u32 s_inodes_count; /* Inodes count */ - u32 s_blocks_count; /* Blocks count */ - u32 s_r_blocks_count; /* Reserved blocks count */ - u32 s_free_blocks_count; /* Free blocks count */ - u32 s_free_inodes_count; /* Free inodes count */ - u32 s_first_data_block; /* First Data Block */ - u32 s_log_block_size; /* Block size */ - u32 s_log_frag_size; /* Fragment size */ - u32 s_blocks_per_group; /* # Blocks per group */ - u32 s_frags_per_group; /* # Fragments per group */ - u32 s_inodes_per_group; /* # Inodes per group */ - u32 s_mtime; /* Mount time */ - u32 s_wtime; /* Write time */ - u16 s_mnt_count; /* Mount count */ - u16 s_max_mnt_count; /* Maximal mount count */ - u16 s_magic; /* Magic signature */ - u16 s_state; /* File system state */ - u16 s_errors; /* Behaviour when detecting errors */ - u16 s_minor_rev_level; /* minor revision level */ - u32 s_lastcheck; /* time of last check */ - u32 s_checkinterval; /* max. time between checks */ - u32 s_creator_os; /* OS */ - u32 s_rev_level; /* Revision level */ - u16 s_def_resuid; /* Default uid for reserved blocks */ - u16 s_def_resgid; /* Default gid for reserved blocks */ + u32 s_inodes_count; /* Inodes count */ + u32 s_blocks_count; /* Blocks count */ + u32 s_r_blocks_count; /* Reserved blocks count */ + u32 s_free_blocks_count; /* Free blocks count */ + u32 s_free_inodes_count; /* Free inodes count */ + u32 s_first_data_block; /* First Data Block */ + u32 s_log_block_size; /* Block size */ + u32 s_log_frag_size; /* Fragment size */ + u32 s_blocks_per_group; /* # Blocks per group */ + u32 s_frags_per_group; /* # Fragments per group */ + u32 s_inodes_per_group; /* # Inodes per group */ + u32 s_mtime; /* Mount time */ + u32 s_wtime; /* Write time */ + u16 s_mnt_count; /* Mount count */ + u16 s_max_mnt_count; /* Maximal mount count */ + u16 s_magic; /* Magic signature */ + u16 s_state; /* File system state */ + u16 s_errors; /* Behaviour when detecting errors */ + u16 s_minor_rev_level; /* minor revision level */ + u32 s_lastcheck; /* time of last check */ + u32 s_checkinterval; /* max. time between checks */ + u32 s_creator_os; /* OS */ + u32 s_rev_level; /* Revision level */ + u16 s_def_resuid; /* Default uid for reserved blocks */ + u16 s_def_resgid; /* Default gid for reserved blocks */ /* * These fields are for EXT2_DYNAMIC_REV superblocks only. * @@ -117,92 +116,89 @@ typedef struct ext2_superblock * feature set, it must abort and not try to meddle with * things it doesn't understand... */ - u32 s_first_ino; /* First non-reserved inode */ - u16 s_inode_size; /* size of inode structure */ - u16 s_block_group_nr; /* block group # of this superblock */ - u32 s_feature_compat; /* compatible feature set */ - u32 s_feature_incompat; /* incompatible feature set */ - u32 s_feature_ro_compat; /* readonly-compatible feature set */ - u8 s_uuid[16]; /* 128-bit uuid for volume */ - char s_volume_name[16]; /* volume name */ - char s_last_mounted[64]; /* directory where last mounted */ - u32 s_algorithm_usage_bitmap; /* For compression */ + u32 s_first_ino; /* First non-reserved inode */ + u16 s_inode_size; /* size of inode structure */ + u16 s_block_group_nr; /* block group # of this superblock */ + u32 s_feature_compat; /* compatible feature set */ + u32 s_feature_incompat; /* incompatible feature set */ + u32 s_feature_ro_compat; /* readonly-compatible feature set */ + u8 s_uuid[16]; /* 128-bit uuid for volume */ + char s_volume_name[16]; /* volume name */ + char s_last_mounted[64]; /* directory where last mounted */ + u32 s_algorithm_usage_bitmap; /* For compression */ /* * Performance hints. Directory preallocation should only * happen if the EXT2_COMPAT_PREALLOC flag is on. */ - u8 s_prealloc_blocks;/* Nr of blocks to try to preallocate*/ - u8 s_prealloc_dir_blocks;/* Nr to preallocate for dirs */ - u16 s_padding1; + u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/ + u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */ + u16 s_padding1; /* * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set. */ - u8 s_journal_uuid[16]; /* uuid of journal superblock */ - u32 s_journal_inum; /* inode number of journal file */ - u32 s_journal_dev; /* device number of journal file */ - u32 s_last_orphan; /* start of list of inodes to delete */ - u32 s_hash_seed[4]; /* HTREE hash seed */ - u8 s_def_hash_version; /* Default hash version to use */ - u8 s_reserved_char_pad; - u16 s_reserved_word_pad; - u32 s_default_mount_opts; - u32 s_first_meta_bg; /* First metablock block group */ - u32 s_reserved[190];/* Padding to the end of the block */ -} SuperBlock,*pSuperBlock; + u8 s_journal_uuid[16]; /* uuid of journal superblock */ + u32 s_journal_inum; /* inode number of journal file */ + u32 s_journal_dev; /* device number of journal file */ + u32 s_last_orphan; /* start of list of inodes to delete */ + u32 s_hash_seed[4]; /* HTREE hash seed */ + u8 s_def_hash_version; /* Default hash version to use */ + u8 s_reserved_char_pad; + u16 s_reserved_word_pad; + u32 s_default_mount_opts; + u32 s_first_meta_bg; /* First metablock block group */ + u32 s_reserved[190]; /* Padding to the end of the block */ +} SuperBlock, *pSuperBlock; extern SuperBlock ext2_sb; - typedef struct ext2_group_descriptor { - u32 bg_block_bitmap; - u32 bg_inode_bitmap; - u32 bg_inode_table; - u16 bg_free_blocks_count; - u16 bg_free_inodes_count; - u16 bg_used_dirs_count; - u16 bg_pad; - u32 bg_reserved[3]; -} GroupDesc,*pGroupDesc; - -#define EXT2_NDIR_BLOCKS (12) -#define EXT2_IND_BLOCK (EXT2_NDIR_BLOCKS) -#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1) -#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) -#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) - + u32 bg_block_bitmap; + u32 bg_inode_bitmap; + u32 bg_inode_table; + u16 bg_free_blocks_count; + u16 bg_free_inodes_count; + u16 bg_used_dirs_count; + u16 bg_pad; + u32 bg_reserved[3]; +} GroupDesc, *pGroupDesc; + +#define EXT2_NDIR_BLOCKS (12) +#define EXT2_IND_BLOCK (EXT2_NDIR_BLOCKS) +#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1) +#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) +#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) typedef struct ext2_inode { - u16 i_mode; - u16 i_uid; - u32 i_size; - u32 i_atime; - u32 i_ctime; - u32 i_mtime; - u32 i_dtime; - u16 i_gid; - u16 i_links_count; - u32 i_blocks; - u32 i_flags; - u32 i_osd1; - u32 i_block[EXT2_N_BLOCKS]; - u32 i_generation; - u32 i_file_acl; - u32 i_dir_acl; - u32 i_faddr; - u8 i_osd2[12]; -} Inode,*pInode; - - -#define EXT2_NAME_LEN 255 + u16 i_mode; + u16 i_uid; + u32 i_size; + u32 i_atime; + u32 i_ctime; + u32 i_mtime; + u32 i_dtime; + u16 i_gid; + u16 i_links_count; + u32 i_blocks; + u32 i_flags; + u32 i_osd1; + u32 i_block[EXT2_N_BLOCKS]; + u32 i_generation; + u32 i_file_acl; + u32 i_dir_acl; + u32 i_faddr; + u8 i_osd2[12]; +} Inode, *pInode; + +#define EXT2_NAME_LEN 255 typedef struct ext2_dir_ent { - u32 inode; - u16 rec_len; - u8 name_len; - u8 file_type; /* 目录类型 */ - char name[EXT2_NAME_LEN]; + u32 inode; + u16 rec_len; + u8 name_len; + u8 file_type; /* 目录类型 */ + char name[EXT2_NAME_LEN]; } DirEnt, *pDirEnt; /* @@ -222,17 +218,14 @@ enum EXT2_FT_MAX }; +#define EXT2_DIR_PAD 4 +#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1) +#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \ + ~EXT2_DIR_ROUND) +#define EXT2_MAX_REC_LEN ((1 << 16) - 1) -#define EXT2_DIR_PAD 4 -#define EXT2_DIR_ROUND (EXT2_DIR_PAD-1) -#define EXT2_DIR_REC_LEN(name_len) (((name_len)+8+EXT2_DIR_ROUND) & \ - ~EXT2_DIR_ROUND) -#define EXT2_MAX_REC_LEN ((1<<16)-1) - - -extern int ext2_read_inode(unsigned int n, pInode inode); -extern int ext2_get_file_inode(const char *path, pInode inode); -extern int ext2_read_file(const pInode ino, void *buf, size_t count); - +extern int ext2_read_inode(unsigned int n, pInode inode); +extern int ext2_get_file_inode(const char *path, pInode inode); +extern int ext2_read_file(const pInode ino, void *buf, size_t count); #endif //_EXT2_H diff --git a/include/bits.h b/include/bits.h index 82c0582..7f49882 100644 --- a/include/bits.h +++ b/include/bits.h @@ -14,23 +14,25 @@ *-------------------------------------------------------------------------- */ -#ifndef _BITS_H +#ifndef _BITS_H #define _BITS_H //#define SET_BIT(bit) (1UL<> 5), "2" (addr), "b" (addr) : "memory"); + : "=a"(res), "=&c"(d0), "=&D"(d1) + : "1"((size + 31) >> 5), "2"(addr), "b"(addr) + : "memory"); return res; } @@ -151,8 +157,9 @@ static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) "1:\tsubl %%ebx,%%edi\n\t" "shll $3,%%edi\n\t" "addl %%edi,%%edx" - :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2) - :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory"); + : "=d"(res), "=&c"(d0), "=&D"(d1), "=&a"(d2) + : "1"((size + 31) >> 5), "2"(addr), "b"(addr) + : "memory"); return res; } @@ -164,19 +171,20 @@ static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) */ static inline int find_next_zero_bit(const unsigned long *addr, int size, int offset) { - unsigned long * p = ((unsigned long *) addr) + (offset >> 5); + unsigned long *p = ((unsigned long *)addr) + (offset >> 5); int set = 0, bit = offset & 31, res; - if (bit) { + if (bit) + { /* * Look for zero in the first 32 bits. */ __asm__("bsfl %1,%0\n\t" - "jne 1f\n\t" - "movl $32, %0\n" - "1:" - : "=r" (set) - : "r" (~(*p >> bit))); + "jne 1f\n\t" + "movl $32, %0\n" + "1:" + : "=r"(set) + : "r"(~(*p >> bit))); if (set < (32 - bit)) return set + offset; set = 32 - bit; @@ -185,7 +193,7 @@ static inline int find_next_zero_bit(const unsigned long *addr, int size, int of /* * No zero yet, search remaining full bytes for a zero */ - res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr)); + res = find_first_zero_bit(p, size - 32 * (p - (unsigned long *)addr)); return (offset + set + res); } @@ -194,9 +202,9 @@ static inline int variable_test_bit(int nr, volatile const unsigned long *addr) int oldbit; asm volatile("bt %2,%1\n\t" - "sbb %0,%0" - : "=r" (oldbit) - : "m" (*(unsigned long *)addr), "Ir" (nr)); + "sbb %0,%0" + : "=r"(oldbit) + : "m"(*(unsigned long *)addr), "Ir"(nr)); return oldbit; } diff --git a/include/boot/boot.h b/include/boot/boot.h index 4fbef9e..8894def 100644 --- a/include/boot/boot.h +++ b/include/boot/boot.h @@ -15,32 +15,35 @@ #ifndef ASM -#define E820_RAM 1 -#define E820_RESERVED 2 -#define E820_ACPI 3 -#define E820_NVS 4 -#define E820_UNUSABLE 5 +#define E820_RAM 1 +#define E820_RESERVED 2 +#define E820_ACPI 3 +#define E820_NVS 4 +#define E820_UNUSABLE 5 -#define E820_MAP_CNT 128 +#define E820_MAP_CNT 128 -struct e820_entry { +struct e820_entry +{ unsigned long addr; unsigned long size; unsigned long type; }; -struct e820map { +struct e820map +{ unsigned long map_cnt; struct e820_entry map[E820_MAP_CNT]; }; -struct boot_params { +struct boot_params +{ char *cmdline; unsigned long boot_device; unsigned long root_device; unsigned long mem_lower; // in bytes - unsigned long mem_upper; + unsigned long mem_upper; struct e820map e820map; }; diff --git a/include/bug.h b/include/bug.h index f932ba2..6548715 100644 --- a/include/bug.h +++ b/include/bug.h @@ -9,9 +9,16 @@ #pragma once -#define BUG() do { \ - printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \ - panic("BUG!"); \ -} while (0) +#define BUG() \ + do \ + { \ + printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \ + panic("BUG!"); \ + } while (0) -#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0) +#define BUG_ON(condition) \ + do \ + { \ + if (unlikely((condition) != 0)) \ + BUG(); \ + } while (0) diff --git a/include/elf.h b/include/elf.h index 90a7c88..1b1e99d 100644 --- a/include/elf.h +++ b/include/elf.h @@ -13,15 +13,15 @@ #ifndef _ELF_H #define _ELF_H -typedef u16 Elf32_Half; -typedef u32 Elf32_Word; -typedef s32 Elf32_Sword; -typedef u64 Elf32_Xword; -typedef s64 Elf32_Sxword; -typedef u32 Elf32_Addr; -typedef u32 Elf32_Off; -typedef u16 Elf32_Section; -typedef Elf32_Half Elf32_Versym; +typedef u16 Elf32_Half; +typedef u32 Elf32_Word; +typedef s32 Elf32_Sword; +typedef u64 Elf32_Xword; +typedef s64 Elf32_Sxword; +typedef u32 Elf32_Addr; +typedef u32 Elf32_Off; +typedef u16 Elf32_Section; +typedef Elf32_Half Elf32_Versym; /* *-------------------------------------------------------------------------- @@ -30,120 +30,117 @@ typedef Elf32_Half Elf32_Versym; *-------------------------------------------------------------------------- */ -#define EI_NIDENT (16) -typedef struct +#define EI_NIDENT (16) +typedef struct { - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; // ELF Header Size in bytes - Elf32_Half e_phentsize; // Program header table entry size - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; //Section Header String Table Index -}Elf32_Ehdr, *pElf32_Ehdr; - -#define ELFMAG "\177ELF" -#define SELFMAG 4 - -#define EI_MAG0 0 -#define ELFMAG0 0x7F - -#define EI_MAG1 1 -#define ELFMAG1 'E' - -#define EI_MAG2 2 -#define ELFMAG2 'L' - -#define EI_MAG3 3 -#define ELFMAG3 'F' - -#define EI_CLASS 4 -#define ELFCLASSNONE 0 -#define ELFCLASS32 1 -#define ELFCLASS64 2 -#define ELFCLASSNUM 3 - -#define EI_DATA 5 -#define ELFDATANONE 0 -#define ELFDATA2LSB 1 /* 2's complement, little endian */ -#define ELFDATA2MSB 2 /* 2's complement, big endian */ -#define ELFDATANUM 3 - -#define EI_VERSION 6 /* value must be EV_CURRENT */ - -#define EI_OSABI 7 -#define ELFOSABI_NONE 0 /* UNIX System V ABI */ -#define ELFOSABI_SYSV 0 /* Alias. */ -#define ELFOSABI_HPUX 1 /* HP-UX */ -#define ELFOSABI_NETBSD 2 /* NetBSD. */ -#define ELFOSABI_LINUX 3 /* Linux. */ -#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ -#define ELFOSABI_AIX 7 /* IBM AIX. */ -#define ELFOSABI_IRIX 8 /* SGI Irix. */ -#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ -#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ -#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ -#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ -#define ELFOSABI_ARM 97 /* ARM */ -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ - -#define EI_ABIVERSION 8 - -#define EI_PAD 9 - + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; // ELF Header Size in bytes + Elf32_Half e_phentsize; // Program header table entry size + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; //Section Header String Table Index +} Elf32_Ehdr, *pElf32_Ehdr; + +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define EI_MAG0 0 +#define ELFMAG0 0x7F + +#define EI_MAG1 1 +#define ELFMAG1 'E' + +#define EI_MAG2 2 +#define ELFMAG2 'L' + +#define EI_MAG3 3 +#define ELFMAG3 'F' + +#define EI_CLASS 4 +#define ELFCLASSNONE 0 +#define ELFCLASS32 1 +#define ELFCLASS64 2 +#define ELFCLASSNUM 3 + +#define EI_DATA 5 +#define ELFDATANONE 0 +#define ELFDATA2LSB 1 /* 2's complement, little endian */ +#define ELFDATA2MSB 2 /* 2's complement, big endian */ +#define ELFDATANUM 3 + +#define EI_VERSION 6 /* value must be EV_CURRENT */ + +#define EI_OSABI 7 +#define ELFOSABI_NONE 0 /* UNIX System V ABI */ +#define ELFOSABI_SYSV 0 /* Alias. */ +#define ELFOSABI_HPUX 1 /* HP-UX */ +#define ELFOSABI_NETBSD 2 /* NetBSD. */ +#define ELFOSABI_LINUX 3 /* Linux. */ +#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ +#define ELFOSABI_AIX 7 /* IBM AIX. */ +#define ELFOSABI_IRIX 8 /* SGI Irix. */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ +#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ +#define ELFOSABI_ARM 97 /* ARM */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ + +#define EI_ABIVERSION 8 + +#define EI_PAD 9 /* values for e_type (object file type). */ -#define ET_NONE 0 /* No file type */ -#define ET_REL 1 /* Relocatable file */ -#define ET_EXEC 2 /* Executable file */ -#define ET_DYN 3 /* Shared object file */ -#define ET_CORE 4 /* Core file */ -#define ET_NUM 5 /* Number of defined types */ -#define ET_LOOS 0xFE00 /* OS-Specific range start */ -#define ET_HIOS 0xFEFF /* OS-Specific range end */ -#define ET_LOPROC 0xFF00 /* Processor-Specific range start */ -#define ET_HIPROC 0xFFFF /* Processor-Specific range end */ - -#define EM_NONE 0 /* No Machine */ -#define EM_386 3 /* Intel 80386 */ -#define EM_MIPS 8 /* MIPS R3000 big endian*/ -#define EM_MIPS_RS3_LE 8 /* MIPS R3000 little endian*/ -#define EM_PPC 20 /* Power PC */ -#define EM_ARM 40 /* ARM */ -#define EM_NUM 95 - +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* Relocatable file */ +#define ET_EXEC 2 /* Executable file */ +#define ET_DYN 3 /* Shared object file */ +#define ET_CORE 4 /* Core file */ +#define ET_NUM 5 /* Number of defined types */ +#define ET_LOOS 0xFE00 /* OS-Specific range start */ +#define ET_HIOS 0xFEFF /* OS-Specific range end */ +#define ET_LOPROC 0xFF00 /* Processor-Specific range start */ +#define ET_HIPROC 0xFFFF /* Processor-Specific range end */ + +#define EM_NONE 0 /* No Machine */ +#define EM_386 3 /* Intel 80386 */ +#define EM_MIPS 8 /* MIPS R3000 big endian*/ +#define EM_MIPS_RS3_LE 8 /* MIPS R3000 little endian*/ +#define EM_PPC 20 /* Power PC */ +#define EM_ARM 40 /* ARM */ +#define EM_NUM 95 /* values for e_version */ -#define EV_NONE 0 -#define EV_CURRENT 1 -#define EV_NUM 2 +#define EV_NONE 0 +#define EV_CURRENT 1 +#define EV_NUM 2 /* *-------------------------------------------------------------------------- * Section header *-------------------------------------------------------------------------- */ -typedef struct +typedef struct { - Elf32_Word sh_name; /* Section name(string tbl index) */ - Elf32_Word sh_type; /* Section type */ - Elf32_Word sh_flags; /* Section flags */ - Elf32_Addr sh_addr; /* virtual addr at execution */ - Elf32_Off sh_offset; /* Section file offset */ - Elf32_Word sh_size; /* Section size in bytes */ - Elf32_Word sh_link; /* Link to another section */ - Elf32_Word sh_info; /* Additional section infomation */ - Elf32_Word sh_addralign; /* Section alignment */ - Elf32_Word sh_entsize; /* Entry size if section holds tbl*/ + Elf32_Word sh_name; /* Section name(string tbl index) */ + Elf32_Word sh_type; /* Section type */ + Elf32_Word sh_flags; /* Section flags */ + Elf32_Addr sh_addr; /* virtual addr at execution */ + Elf32_Off sh_offset; /* Section file offset */ + Elf32_Word sh_size; /* Section size in bytes */ + Elf32_Word sh_link; /* Link to another section */ + Elf32_Word sh_info; /* Additional section infomation */ + Elf32_Word sh_addralign; /* Section alignment */ + Elf32_Word sh_entsize; /* Entry size if section holds tbl*/ } Elf32_Shdr, *pElf32_Shdr; - /* *-------------------------------------------------------------------------- * Program segment header @@ -152,36 +149,36 @@ typedef struct typedef struct { - Elf32_Word p_type; - Elf32_Off p_offset; - Elf32_Addr p_vaddr; - Elf32_Addr p_paddr; - Elf32_Word p_filesz; - Elf32_Word p_memsz; - Elf32_Word p_flags; - Elf32_Word p_align; -} Elf32_Phdr,*pElf32_Phdr; + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; +} Elf32_Phdr, *pElf32_Phdr; /* p_type */ -#define PT_NULL 0 /* Program header table entry unused */ -#define PT_LOAD 1 /* Loadable program segment */ -#define PT_DYNAMIC 2 /* Dynamic linking information */ -#define PT_INTERP 3 /* Program interpreter */ -#define PT_NOTE 4 /* Auxiliary information */ -#define PT_SHLIB 5 /* Reserved */ -#define PT_PHDR 6 /* Entry for header table itself */ -#define PT_TLS 7 /* Thread-local storage segment */ -#define PT_NUM 8 /* Number of defined types */ -#define PT_LOOS 0x60000000 /* Start of OS-Specific */ -#define PT_GNU_EH_FRAME 0x6474E550 /* GCC .eh_frame_hdr segment */ -#define PT_GNU_STACK 0x6474E551 /* Indicates stack executability */ -#define PT_GNU_RELRO 0x6474E552 /* Read-only after relocation */ -#define PT_LOSUNW 0x6FFFFFFA -#define PT_SHNWBSS 0x6FFFFFFA /* Sun Specific Segment */ -#define PT_SUNWSTACK 0x6FFFFFFB /* Stack segment */ -#define PT_HISUNW 0x6FFFFFFF -#define PT_HIOS 0x6FFFFFFF /* End of OS-Specific */ -#define PT_LOPROC 0x70000000 /* Start of processor-specific */ -#define PT_HIPROC 0x7FFFFFFF /* End of processor-specific */ +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ +#define PT_NOTE 4 /* Auxiliary information */ +#define PT_SHLIB 5 /* Reserved */ +#define PT_PHDR 6 /* Entry for header table itself */ +#define PT_TLS 7 /* Thread-local storage segment */ +#define PT_NUM 8 /* Number of defined types */ +#define PT_LOOS 0x60000000 /* Start of OS-Specific */ +#define PT_GNU_EH_FRAME 0x6474E550 /* GCC .eh_frame_hdr segment */ +#define PT_GNU_STACK 0x6474E551 /* Indicates stack executability */ +#define PT_GNU_RELRO 0x6474E552 /* Read-only after relocation */ +#define PT_LOSUNW 0x6FFFFFFA +#define PT_SHNWBSS 0x6FFFFFFA /* Sun Specific Segment */ +#define PT_SUNWSTACK 0x6FFFFFFB /* Stack segment */ +#define PT_HISUNW 0x6FFFFFFF +#define PT_HIOS 0x6FFFFFFF /* End of OS-Specific */ +#define PT_LOPROC 0x70000000 /* Start of processor-specific */ +#define PT_HIPROC 0x7FFFFFFF /* End of processor-specific */ #endif //_ELF_H diff --git a/include/errno.h b/include/errno.h index 93f3f00..1c0778c 100644 --- a/include/errno.h +++ b/include/errno.h @@ -11,41 +11,41 @@ #ifndef _ERRNO_H #define _ERRNO_H -#define EPERM 1 /* Operation not permitted */ -#define ENOENT 2 /* No such file or directory */ -#define ESRCH 3 /* No such process */ -#define EINTR 4 /* Interrupted system call */ -#define EIO 5 /* I/O error */ -#define ENXIO 6 /* No such device or address */ -#define E2BIG 7 /* Argument list too long */ -#define ENOEXEC 8 /* Exec format error */ -#define EBADF 9 /* Bad file number */ -#define ECHILD 10 /* No child processes */ -#define EAGAIN 11 /* Try again */ -#define ENOMEM 12 /* Out of memory */ -#define EACCES 13 /* Permission denied */ -#define EFAULT 14 /* Bad address */ -#define ENOTBLK 15 /* Block device required */ -#define EBUSY 16 /* Device or resource busy */ -#define EEXIST 17 /* File exists */ -#define EXDEV 18 /* Cross-device link */ -#define ENODEV 19 /* No such device */ -#define ENOTDIR 20 /* Not a directory */ -#define EISDIR 21 /* Is a directory */ -#define EINVAL 22 /* Invalid argument */ -#define ENFILE 23 /* File table overflow */ -#define EMFILE 24 /* Too many open files */ -#define ENOTTY 25 /* Not a typewriter */ -#define ETXTBSY 26 /* Text file busy */ -#define EFBIG 27 /* File too large */ -#define ENOSPC 28 /* No space left on device */ -#define ESPIPE 29 /* Illegal seek */ -#define EROFS 30 /* Read-only file system */ -#define EMLINK 31 /* Too many links */ -#define EPIPE 32 /* Broken pipe */ -#define EDOM 33 /* Math argument out of domain of func */ -#define ERANGE 34 /* Math result not representable */ +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ -extern int errno; +extern int errno; #endif //_ERRNO_H diff --git a/include/ext2.h b/include/ext2.h index bc20046..7a8b29b 100644 --- a/include/ext2.h +++ b/include/ext2.h @@ -19,43 +19,43 @@ #include -#define EXT2_SUPER_MAGIC 0xEF53 +#define EXT2_SUPER_MAGIC 0xEF53 -#define EXT2_SB_OFFSET 1024 +#define EXT2_SB_OFFSET 1024 -#define EXT2_BAD_INO 1 -#define EXT2_ROOT_INO 2 -#define EXT2_BOOT_LOADER_INO 5 -#define EXT2_UNDEL_DIR_INO 6 +#define EXT2_BAD_INO 1 +#define EXT2_ROOT_INO 2 +#define EXT2_BOOT_LOADER_INO 5 +#define EXT2_UNDEL_DIR_INO 6 -#define EXT2_MIN_BLOCK_SIZE 1024 -#define EXT2_MAX_BLOCK_SIZE 4096 +#define EXT2_MIN_BLOCK_SIZE 1024 +#define EXT2_MAX_BLOCK_SIZE 4096 #define EXT2_MIN_BLOCK_LOG_SIZE 10 -#define EXT2_SB (&ext2_fs.ext2_sb) -#define EXT2_GD (ext2_fs.ext2_gd) +#define EXT2_SB (&ext2_fs.ext2_sb) +#define EXT2_GD (ext2_fs.ext2_gd) unsigned long ext2_block_size(); #define EXT2_BLOCK_SIZE ext2_block_size() //#define EXT2_BLOCK_SIZE (EXT2_MIN_BLOCK_SIZE << (EXT2_SB)->s_log_block_size) -#define EXT2_SECT_PER_BLOCK (EXT2_BLOCK_SIZE/512) +#define EXT2_SECT_PER_BLOCK (EXT2_BLOCK_SIZE / 512) -#define EXT2_BLOCK_SIZE_BITS ((EXT2_SB)->s_log_block_size + 10) -#define EXT2_INODE_SIZE ((EXT2_SB)->s_inode_size) -#define EXT2_INODES_PER_BLOCK (EXT2_BLOCK_SIZE/EXT2_INODE_SIZE) -#define EXT2_FIRST_INO ((EXT2_SB)->s_first_ino) +#define EXT2_BLOCK_SIZE_BITS ((EXT2_SB)->s_log_block_size + 10) +#define EXT2_INODE_SIZE ((EXT2_SB)->s_inode_size) +#define EXT2_INODES_PER_BLOCK (EXT2_BLOCK_SIZE / EXT2_INODE_SIZE) +#define EXT2_FIRST_INO ((EXT2_SB)->s_first_ino) /* * 表示第一个块号. 因为SuperBlock总是从第三个扇区开始的所以如果块的大小 * 是1024的话SuperBlock的块号是1.而如果块的大小是2048或4096则SuperBlock * 的块号是0 */ -#define EXT2_FIRST_BLOCK_ID (EXT2_BLOCK_SIZE == 1024) +#define EXT2_FIRST_BLOCK_ID (EXT2_BLOCK_SIZE == 1024) -#define EXT2_BLOCKS_PER_GROUP ((EXT2_SB)->s_blocks_per_group) -#define EXT2_DESC_PER_BLOCK ((EXT2_SB)->s_desc_per_block) -#define EXT2_INODES_PER_GROUP ((EXT2_SB)->s_inodes_per_group) -#define EXT2_INODES_COUNT ((EXT2_SB)->s_inodes_count) +#define EXT2_BLOCKS_PER_GROUP ((EXT2_SB)->s_blocks_per_group) +#define EXT2_DESC_PER_BLOCK ((EXT2_SB)->s_desc_per_block) +#define EXT2_INODES_PER_GROUP ((EXT2_SB)->s_inodes_per_group) +#define EXT2_INODES_COUNT ((EXT2_SB)->s_inodes_count) /* * ------------------------------------------------------------------------ @@ -64,31 +64,31 @@ unsigned long ext2_block_size(); */ typedef struct ext2_superblock { - u32 s_inodes_count; /* Inodes count */ - u32 s_blocks_count; /* Blocks count */ - u32 s_r_blocks_count; /* Reserved blocks count */ - u32 s_free_blocks_count; /* Free blocks count */ - u32 s_free_inodes_count; /* Free inodes count */ - u32 s_first_data_block; /* First Data Block */ - u32 s_log_block_size; /* Block size */ - u32 s_log_frag_size; /* Fragment size */ - u32 s_blocks_per_group; /* # Blocks per group */ - u32 s_frags_per_group; /* # Fragments per group */ - u32 s_inodes_per_group; /* # Inodes per group */ - u32 s_mtime; /* Mount time */ - u32 s_wtime; /* Write time */ - u16 s_mnt_count; /* Mount count */ - u16 s_max_mnt_count; /* Maximal mount count */ - u16 s_magic; /* Magic signature */ - u16 s_state; /* File system state */ - u16 s_errors; /* Behaviour when detecting errors */ - u16 s_minor_rev_level; /* minor revision level */ - u32 s_lastcheck; /* time of last check */ - u32 s_checkinterval; /* max. time between checks */ - u32 s_creator_os; /* OS */ - u32 s_rev_level; /* Revision level */ - u16 s_def_resuid; /* Default uid for reserved blocks */ - u16 s_def_resgid; /* Default gid for reserved blocks */ + u32 s_inodes_count; /* Inodes count */ + u32 s_blocks_count; /* Blocks count */ + u32 s_r_blocks_count; /* Reserved blocks count */ + u32 s_free_blocks_count; /* Free blocks count */ + u32 s_free_inodes_count; /* Free inodes count */ + u32 s_first_data_block; /* First Data Block */ + u32 s_log_block_size; /* Block size */ + u32 s_log_frag_size; /* Fragment size */ + u32 s_blocks_per_group; /* # Blocks per group */ + u32 s_frags_per_group; /* # Fragments per group */ + u32 s_inodes_per_group; /* # Inodes per group */ + u32 s_mtime; /* Mount time */ + u32 s_wtime; /* Write time */ + u16 s_mnt_count; /* Mount count */ + u16 s_max_mnt_count; /* Maximal mount count */ + u16 s_magic; /* Magic signature */ + u16 s_state; /* File system state */ + u16 s_errors; /* Behaviour when detecting errors */ + u16 s_minor_rev_level; /* minor revision level */ + u32 s_lastcheck; /* time of last check */ + u32 s_checkinterval; /* max. time between checks */ + u32 s_creator_os; /* OS */ + u32 s_rev_level; /* Revision level */ + u16 s_def_resuid; /* Default uid for reserved blocks */ + u16 s_def_resgid; /* Default gid for reserved blocks */ /* * These fields are for EXT2_DYNAMIC_REV superblocks only. * @@ -102,88 +102,87 @@ typedef struct ext2_superblock * feature set, it must abort and not try to meddle with * things it doesn't understand... */ - u32 s_first_ino; /* First non-reserved inode */ - u16 s_inode_size; /* size of inode structure */ - u16 s_block_group_nr; /* block group # of this superblock */ - u32 s_feature_compat; /* compatible feature set */ - u32 s_feature_incompat; /* incompatible feature set */ - u32 s_feature_ro_compat; /* readonly-compatible feature set */ - u8 s_uuid[16]; /* 128-bit uuid for volume */ - char s_volume_name[16]; /* volume name */ - char s_last_mounted[64]; /* directory where last mounted */ - u32 s_algorithm_usage_bitmap; /* For compression */ + u32 s_first_ino; /* First non-reserved inode */ + u16 s_inode_size; /* size of inode structure */ + u16 s_block_group_nr; /* block group # of this superblock */ + u32 s_feature_compat; /* compatible feature set */ + u32 s_feature_incompat; /* incompatible feature set */ + u32 s_feature_ro_compat; /* readonly-compatible feature set */ + u8 s_uuid[16]; /* 128-bit uuid for volume */ + char s_volume_name[16]; /* volume name */ + char s_last_mounted[64]; /* directory where last mounted */ + u32 s_algorithm_usage_bitmap; /* For compression */ /* * Performance hints. Directory preallocation should only * happen if the EXT2_COMPAT_PREALLOC flag is on. */ - u8 s_prealloc_blocks;/* Nr of blocks to try to preallocate*/ - u8 s_prealloc_dir_blocks;/* Nr to preallocate for dirs */ - u16 s_padding1; + u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/ + u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */ + u16 s_padding1; /* * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set. */ - u8 s_journal_uuid[16]; /* uuid of journal superblock */ - u32 s_journal_inum; /* inode number of journal file */ - u32 s_journal_dev; /* device number of journal file */ - u32 s_last_orphan; /* start of list of inodes to delete */ - u32 s_hash_seed[4]; /* HTREE hash seed */ - u8 s_def_hash_version; /* Default hash version to use */ - u8 s_reserved_char_pad; - u16 s_reserved_word_pad; - u32 s_default_mount_opts; - u32 s_first_meta_bg; /* First metablock block group */ - u32 s_reserved[190]; /* Padding to the end of the block */ + u8 s_journal_uuid[16]; /* uuid of journal superblock */ + u32 s_journal_inum; /* inode number of journal file */ + u32 s_journal_dev; /* device number of journal file */ + u32 s_last_orphan; /* start of list of inodes to delete */ + u32 s_hash_seed[4]; /* HTREE hash seed */ + u8 s_def_hash_version; /* Default hash version to use */ + u8 s_reserved_char_pad; + u16 s_reserved_word_pad; + u32 s_default_mount_opts; + u32 s_first_meta_bg; /* First metablock block group */ + u32 s_reserved[190]; /* Padding to the end of the block */ } ext2_sb_t; typedef struct ext2_group_descriptor { - u32 bg_block_bitmap; - u32 bg_inode_bitmap; - u32 bg_inode_table; - u16 bg_free_blocks_count; - u16 bg_free_inodes_count; - u16 bg_used_dirs_count; - u16 bg_pad; - u32 bg_reserved[3]; + u32 bg_block_bitmap; + u32 bg_inode_bitmap; + u32 bg_inode_table; + u16 bg_free_blocks_count; + u16 bg_free_inodes_count; + u16 bg_used_dirs_count; + u16 bg_pad; + u32 bg_reserved[3]; } ext2_gd_t; -#define EXT2_NDIR_BLOCKS (12) -#define EXT2_IND_BLOCK (EXT2_NDIR_BLOCKS) -#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1) -#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) -#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) +#define EXT2_NDIR_BLOCKS (12) +#define EXT2_IND_BLOCK (EXT2_NDIR_BLOCKS) +#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1) +#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) +#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) typedef struct ext2_inode { - u16 i_mode; - u16 i_uid; - u32 i_size; - u32 i_atime; - u32 i_ctime; - u32 i_mtime; - u32 i_dtime; - u16 i_gid; - u16 i_links_count; - u32 i_blocks; - u32 i_flags; - u32 i_osd1; - u32 i_block[EXT2_N_BLOCKS]; - u32 i_generation; - u32 i_file_acl; - u32 i_dir_acl; - u32 i_faddr; - u8 i_osd2[12]; -} ext2_inode_t; - - -#define EXT2_NAME_LEN 255 + u16 i_mode; + u16 i_uid; + u32 i_size; + u32 i_atime; + u32 i_ctime; + u32 i_mtime; + u32 i_dtime; + u16 i_gid; + u16 i_links_count; + u32 i_blocks; + u32 i_flags; + u32 i_osd1; + u32 i_block[EXT2_N_BLOCKS]; + u32 i_generation; + u32 i_file_acl; + u32 i_dir_acl; + u32 i_faddr; + u8 i_osd2[12]; +} ext2_inode_t; + +#define EXT2_NAME_LEN 255 typedef struct ext2_dir_ent { - u32 inode; - u16 rec_len; - u8 name_len; - u8 file_type; - char name[EXT2_NAME_LEN]; + u32 inode; + u16 rec_len; + u8 name_len; + u8 file_type; + char name[EXT2_NAME_LEN]; } ext2_dirent_t; /* @@ -203,12 +202,10 @@ enum EXT2_FT_MAX }; - -#define EXT2_DIR_PAD 4 -#define EXT2_DIR_ROUND (EXT2_DIR_PAD-1) -#define EXT2_DIR_REC_LEN(name_len) (((name_len)+8+EXT2_DIR_ROUND) & ~EXT2_DIR_ROUND) -#define EXT2_MAX_REC_LEN ((1<<16)-1) - +#define EXT2_DIR_PAD 4 +#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1) +#define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & ~EXT2_DIR_ROUND) +#define EXT2_MAX_REC_LEN ((1 << 16) - 1) void ext2_read_inode(unsigned int ino, ext2_inode_t *inode); void ext2_read_file(const ext2_inode_t *inode, char *buf); diff --git a/include/fcntl.h b/include/fcntl.h index 5a0e72f..5aee2cd 100644 --- a/include/fcntl.h +++ b/include/fcntl.h @@ -10,23 +10,22 @@ *-------------------------------------------------------------------------- */ -#ifndef _FCNTL_H +#ifndef _FCNTL_H #define _FCNTL_H -#define O_ACCMODE 0003 -#define O_RDONLY 00 -#define O_WRONLY 01 -#define O_RDWR 02 -#define O_CREAT 0100 /* not fcntl */ -#define O_EXCL 0200 /* not fcntl */ -#define O_NOCTTY 0400 /* not fcntl */ -#define O_TRUNC 01000 /* not fcntl */ -#define O_APPEND 02000 -#define O_NONBLOCK 04000 -#define O_NDELAY O_NONBLOCK -#define O_SYNC 010000 -#define O_FSYNC O_SYNC -#define O_ASYNC 020000 - +#define O_ACCMODE 0003 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 +#define O_CREAT 0100 /* not fcntl */ +#define O_EXCL 0200 /* not fcntl */ +#define O_NOCTTY 0400 /* not fcntl */ +#define O_TRUNC 01000 /* not fcntl */ +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_NDELAY O_NONBLOCK +#define O_SYNC 010000 +#define O_FSYNC O_SYNC +#define O_ASYNC 020000 #endif //_FCNTL_H diff --git a/include/fs.h b/include/fs.h index daf312e..9cf99c6 100644 --- a/include/fs.h +++ b/include/fs.h @@ -17,61 +17,56 @@ #include /* 分区表开始的位置 */ -#define PARTS_POS 0x1BE +#define PARTS_POS 0x1BE /* 设备的主设备号. 占用两个字节. */ -#define DEV_MAJOR_UNUSED 0x0000 -#define DEV_MAJOR_MEM 0x0001 -#define DEV_MAJOR_TTY 0x0002 -#define DEV_MAJOR_IDE0 0x0003 -#define DEV_MAJOR_HDA DEV_MAJOR_IDE0 -#define DEV_MAJOR_IDE1 0x0004 -#define DEV_MAJOR_SCSI0 0x0005 -#define DEV_MAJOR_SCSI2 0x0006 +#define DEV_MAJOR_UNUSED 0x0000 +#define DEV_MAJOR_MEM 0x0001 +#define DEV_MAJOR_TTY 0x0002 +#define DEV_MAJOR_IDE0 0x0003 +#define DEV_MAJOR_HDA DEV_MAJOR_IDE0 +#define DEV_MAJOR_IDE1 0x0004 +#define DEV_MAJOR_SCSI0 0x0005 +#define DEV_MAJOR_SCSI2 0x0006 -#define DEV_MAJOR_BITS (16) -#define DEV_MINOR_MASK ((1UL << DEV_MAJOR_BITS) - 1) - -#define MAKE_DEV(major, minor) ((major) << DEV_MAJOR_BITS | minor) - -#define DEV_MAJOR(dev) ((unsigned int)((dev) >> DEV_MAJOR_BITS)) -#define DEV_MINOR(dev) ((unsigned int)((dev) & DEV_MINOR_MASK)) +#define DEV_MAJOR_BITS (16) +#define DEV_MINOR_MASK ((1UL << DEV_MAJOR_BITS) - 1) +#define MAKE_DEV(major, minor) ((major) << DEV_MAJOR_BITS | minor) +#define DEV_MAJOR(dev) ((unsigned int)((dev) >> DEV_MAJOR_BITS)) +#define DEV_MINOR(dev) ((unsigned int)((dev)&DEV_MINOR_MASK)) //#define MAX_SUPT_FILE_SIZE (1) -#define NR_FILES (1) -#define NR_OPENS (1) +#define NR_FILES (1) +#define NR_OPENS (1) unsigned int namei(const char *path); -#define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK*EXT2_BLOCK_SIZE) - - +#define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK * EXT2_BLOCK_SIZE) typedef struct chrdev { int (*read)(char *buf, size_t count); } chrdev_t; -enum { +enum +{ CHRDEV_CNSL, CHRDEV_SIZE }; extern chrdev_t *chrdev[]; -typedef struct +typedef struct { - -} file_t; - +} file_t; #if 0 -#define NR_FILES (PAGE_SIZE/sizeof(File)) -#define NR_INODES (2*NR_FILES) -#define NR_OPENS (2) /* 一个进程同时打开文件的限制数 */ +#define NR_FILES (PAGE_SIZE / sizeof(File)) +#define NR_INODES (2 * NR_FILES) +#define NR_OPENS (2) /* 一个进程同时打开文件的限制数 */ extern File file_table[NR_FILES]; extern Inode inode_table[NR_INODES]; diff --git a/include/global.h b/include/global.h index 00c6374..52b4e1a 100644 --- a/include/global.h +++ b/include/global.h @@ -10,12 +10,11 @@ *-------------------------------------------------------------------------- */ -#ifndef _GLOBAL_H +#ifndef _GLOBAL_H #define _GLOBAL_H - -#ifndef __STRING -#define __STRING(x) #x +#ifndef __STRING +#define __STRING(x) #x #endif #endif //_GLOBAL_H diff --git a/include/i8259.h b/include/i8259.h index af6f0a0..048b6b9 100644 --- a/include/i8259.h +++ b/include/i8259.h @@ -21,15 +21,15 @@ #include "irq.h" #include "io.h" -#define PIC_MASTER_CMD 0x20 -#define PIC_MASTER_IMR 0x21 -#define PIC_SLAVE_CMD 0xA0 -#define PIC_SLAVE_IMR 0xA1 +#define PIC_MASTER_CMD 0x20 +#define PIC_MASTER_IMR 0x21 +#define PIC_SLAVE_CMD 0xA0 +#define PIC_SLAVE_IMR 0xA1 -#define PIC_MASTER_ISR PIC_MASTER_CMD -#define PIC_SLAVE_ISR PIC_SLAVE_CMD +#define PIC_MASTER_ISR PIC_MASTER_CMD +#define PIC_SLAVE_ISR PIC_SLAVE_CMD -#define PIC_CASCADE_IR 0x2 //The IR2 on Master Connect to Slave. +#define PIC_CASCADE_IR 0x2 //The IR2 on Master Connect to Slave. #define PIC_AEOI 0 diff --git a/include/init.h b/include/init.h index a23c6c8..60135b7 100644 --- a/include/init.h +++ b/include/init.h @@ -9,6 +9,4 @@ #pragma once - -#define __initdata __attribute__ ((__section__ (".init.data"))) - +#define __initdata __attribute__((__section__(".init.data"))) diff --git a/include/io.h b/include/io.h index 26ed9fd..abd45f7 100644 --- a/include/io.h +++ b/include/io.h @@ -19,69 +19,72 @@ #include -#define inb(port)({ \ -u8 _bt; \ -asm("inb %%dx,%%al" \ -:"=a" (_bt) \ -:"d" (port)); \ -_bt; \ -}) - -#define inw(port)({ \ -u16 _bt; \ -asm("inw %%dx,%%ax" \ -:"=a" (_bt) \ -:"d" (port)); \ -_bt; \ -}) +#define inb(port) ( \ + { \ + u8 _bt; \ + asm("inb %%dx,%%al" \ + : "=a"(_bt) \ + : "d"(port)); \ + _bt; \ + }) -#define inl(port)({ \ -u32 _bt; \ -asm("inl %%dx,%%eax" \ -:"=a" (_bt) \ -:"d" (port)); \ -_bt; \ -}) +#define inw(port) ( \ + { \ + u16 _bt; \ + asm("inw %%dx,%%ax" \ + : "=a"(_bt) \ + : "d"(port)); \ + _bt; \ + }) +#define inl(port) ( \ + { \ + u32 _bt; \ + asm("inl %%dx,%%eax" \ + : "=a"(_bt) \ + : "d"(port)); \ + _bt; \ + }) -#define outb(value, port)({ \ -__asm__("outb %%al,%%dx" \ -: \ -:"a" (value),"d" (port)); \ +#define outb(value, port) ({ \ + __asm__("outb %%al,%%dx" \ + : \ + : "a"(value), "d"(port)); \ }) -#define outw(value, port)({ \ -__asm__("outw %%ax,%%dx" \ -: \ -:"a" (value),"d" (port)); \ -}) -#define outl(value, port)({ \ -__asm__("outl %%eax,%%dx" \ -: \ -:"a" (value),"d" (port)); \ +#define outw(value, port) ({ \ + __asm__("outw %%ax,%%dx" \ + : \ + : "a"(value), "d"(port)); \ }) - -#define outb_p(value, port)({ \ -__asm__("outb %%al,%%dx;nop;nop;nop;nop" \ -: \ -:"a" (value),"d" (port)); \ +#define outl(value, port) ({ \ + __asm__("outl %%eax,%%dx" \ + : \ + : "a"(value), "d"(port)); \ }) -#define inb_p(port)({ \ -u8 _bt; \ -__asm__("inb %%dx,%%al;nop;nop;nop;nop" \ -:"=a" (_bt) \ -:"d" (port)); \ -_bt; \ +#define outb_p(value, port) ({ \ + __asm__("outb %%al,%%dx;nop;nop;nop;nop" \ + : \ + : "a"(value), "d"(port)); \ }) -#define BUILDIO(bwl, type) \ -static inline void ins##bwl(int port, void *buf, unsigned long count) \ -{ \ - asm volatile("cld;rep;ins" #bwl \ - : "+c"(count), "+D"(buf) : "d"(port)); \ -} +#define inb_p(port) ( \ + { \ + u8 _bt; \ + __asm__("inb %%dx,%%al;nop;nop;nop;nop" \ + : "=a"(_bt) \ + : "d"(port)); \ + _bt; \ + }) +#define BUILDIO(bwl, type) \ + static inline void ins##bwl(int port, void *buf, unsigned long count) \ + { \ + asm volatile("cld;rep;ins" #bwl \ + : "+c"(count), "+D"(buf) \ + : "d"(port)); \ + } BUILDIO(b, char) BUILDIO(w, short) diff --git a/include/irq.h b/include/irq.h index d135684..bcaaf1b 100644 --- a/include/irq.h +++ b/include/irq.h @@ -19,42 +19,42 @@ #include "system.h" -#define FIRST_IRQ_VECT 0x20 -#define NR_IRQS (0xFF-FIRST_IRQ_VECT) +#define FIRST_IRQ_VECT 0x20 +#define NR_IRQS (0xFF - FIRST_IRQ_VECT) typedef struct irq_chip { const char *name; - int (*enable)(unsigned int irq); - int (*disable)(unsigned int irq); - void (*ack)(unsigned int irq); + int (*enable)(unsigned int irq); + int (*disable)(unsigned int irq); + void (*ack)(unsigned int irq); } irq_chip_t; typedef struct irqaction { //void (*handler)(pt_regs_t * regs, unsigned int irq); - void (*handler)(unsigned int irq, pt_regs_t * regs, void *dev_id); + void (*handler)(unsigned int irq, pt_regs_t *regs, void *dev_id); const char *dev_name; void *dev_id; - struct irqaction *next; + struct irqaction *next; } irq_action_t; typedef struct irq_desc { - irq_chip_t * chip; - irq_action_t * action; - unsigned int status; - unsigned int depth; + irq_chip_t *chip; + irq_action_t *action; + unsigned int status; + unsigned int depth; } irq_desc_t; -extern irq_chip_t i8259_chip; -extern irq_desc_t irq_desc[]; -extern irq_desc_t no_irq_desc; -int request_irq(unsigned int irq, - //void (*handler)(pt_regs_t *, unsigned int), - void (*handler)(unsigned int, pt_regs_t *, void *), - const char *devname, - void *dev_id); +extern irq_chip_t i8259_chip; +extern irq_desc_t irq_desc[]; +extern irq_desc_t no_irq_desc; +int request_irq(unsigned int irq, + //void (*handler)(pt_regs_t *, unsigned int), + void (*handler)(unsigned int, pt_regs_t *, void *), + const char *devname, + void *dev_id); int open_irq(unsigned int irq); int close_irq(unsigned int irq); @@ -62,13 +62,15 @@ int close_irq(unsigned int irq); #define enable_irq() asm("sti") #define disable_irq() asm("cli") -#define irq_save(x) __asm__ __volatile__("pushfl; popl %0; cli":"=g"(x)::"memory") - -#define irq_restore(x) do { \ - typecheck(unsigned long, x); \ - __asm__ __volatile__("pushl %0; popfl"::"g"(x):"memory", "cc"); \ -} while(0) - +#define irq_save(x) __asm__ __volatile__("pushfl; popl %0; cli" \ + : "=g"(x)::"memory") +#define irq_restore(x) \ + do \ + { \ + typecheck(unsigned long, x); \ + __asm__ __volatile__("pushl %0; popfl" ::"g"(x) \ + : "memory", "cc"); \ + } while (0) #endif //_IRQ_H diff --git a/include/linkage.h b/include/linkage.h index ccbe68d..d672128 100644 --- a/include/linkage.h +++ b/include/linkage.h @@ -13,16 +13,14 @@ * *-------------------------------------------------------------------------- */ -#ifndef __LINKAGE_H +#ifndef __LINKAGE_H #define __LINKAGE_H -#define ALIGN .align 0x04,0x90 -#define ALIGN_STR ".align 0x04,0x90" -#define ENTRY(symbol) \ - .global symbol; \ - ALIGN; \ +#define ALIGN .align 0x04, 0x90 +#define ALIGN_STR ".align 0x04,0x90" +#define ENTRY(symbol) \ + .global symbol; \ + ALIGN; \ symbol: - - #endif diff --git a/include/list.h b/include/list.h index f3118ce..66d8ebe 100644 --- a/include/list.h +++ b/include/list.h @@ -25,43 +25,45 @@ typedef struct list_head // TODO Remove typedef list_head_t ListHead, *pListHead; -#define LIST_HEAD_INIT(name) {&(name), &(name) } +#define LIST_HEAD_INIT(name) \ + { \ + &(name), &(name) \ + } #define LIST_HEAD(name) list_head_t name = LIST_HEAD_INIT(name) -#define INIT_LIST_HEAD(ptr) \ -do{ \ - (ptr)->next = (ptr); \ - (ptr)->prev = (ptr); \ -}while(0) +#define INIT_LIST_HEAD(ptr) \ + do \ + { \ + (ptr)->next = (ptr); \ + (ptr)->prev = (ptr); \ + } while (0) -#define list_entry(ptr, type, member) \ - ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) +#define list_entry(ptr, type, member) \ + ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) #define list_first_entry(ptr, type, member) \ list_entry((ptr)->next, type, member) +#define list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) -#define list_for_each(pos, head) \ - for(pos = (head)->next; pos != (head); pos = pos->next) - -#define list_for_each_safe(pos, tmp, head) \ - for(pos = (head)->next, tmp = pos->next;\ - pos != (head); \ - pos = tmp, tmp = pos->next) - -#define list_for_each_entry_safe(pos, tmp, head, member) \ - for(pos=list_entry((head)->next, typeof(*pos), member), \ - tmp=list_entry(pos->member.next, typeof(*pos), member); \ - &pos->member != (head); \ - pos=tmp, tmp=list_entry(tmp->member.next, typeof(*tmp), member)) +#define list_for_each_safe(pos, tmp, head) \ + for (pos = (head)->next, tmp = pos->next; \ + pos != (head); \ + pos = tmp, tmp = pos->next) +#define list_for_each_entry_safe(pos, tmp, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member), \ + tmp = list_entry(pos->member.next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = tmp, tmp = list_entry(tmp->member.next, typeof(*tmp), member)) static inline void _list_add(list_head_t *pnew, list_head_t *prev, list_head_t *next) { - next->prev = pnew; - pnew->next = next; - pnew->prev = prev; - prev->next = pnew; + next->prev = pnew; + pnew->next = next; + pnew->prev = prev; + prev->next = pnew; } static inline void list_add(list_head_t *pnew, list_head_t *head) @@ -74,7 +76,6 @@ static inline void list_add_tail(list_head_t *pnew, list_head_t *head) _list_add(pnew, head->prev, head); } - static inline void _list_del(list_head_t *prev, list_head_t *next) { next->prev = prev; diff --git a/include/mm.h b/include/mm.h index 07620fb..10d4f45 100644 --- a/include/mm.h +++ b/include/mm.h @@ -20,6 +20,5 @@ unsigned long bootmem_page_state(unsigned long pfn); #define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE) - kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align); void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags); diff --git a/include/msr.h b/include/msr.h index 725caf2..0a80e2d 100644 --- a/include/msr.h +++ b/include/msr.h @@ -14,16 +14,17 @@ *-------------------------------------------------------------------------- */ -#ifndef _MSR_H +#ifndef _MSR_H #define _MSR_H -#define MSR_SYSENTER_CS 0x174 -#define MSR_SYSENTER_ESP 0x175 -#define MSR_SYSENTER_EIP 0x176 - -#define wrmsr(msr, lowval, highval) do{\ - asm("wrmsr;"::"c"(msr),"a"(lowval),"d"(highval));\ -}while(0); +#define MSR_SYSENTER_CS 0x174 +#define MSR_SYSENTER_ESP 0x175 +#define MSR_SYSENTER_EIP 0x176 +#define wrmsr(msr, lowval, highval) \ + do \ + { \ + asm("wrmsr;" ::"c"(msr), "a"(lowval), "d"(highval)); \ + } while (0); #endif //_MSR_H diff --git a/include/page.h b/include/page.h index 0e94cec..7d84f70 100644 --- a/include/page.h +++ b/include/page.h @@ -17,65 +17,64 @@ #ifndef _PAGE_H #define _PAGE_H -#define PAGE_P 0x1 -#define PAGE_WR 0x2 -#define PAGE_US 0x4 +#define PAGE_P 0x1 +#define PAGE_WR 0x2 +#define PAGE_US 0x4 -#define PAGE_SHIFT (12) -#define PAGE_SIZE (1UL << PAGE_SHIFT) -#define PAGE_MASK (~((1UL << PAGE_SHIFT)-1)) -#define PAGE_OFFSET (0xC0000000) -#define PAGE_PDE_CNT 1024 -#define PAGE_PTE_CNT 1024 +#define PAGE_SHIFT (12) +#define PAGE_SIZE (1UL << PAGE_SHIFT) +#define PAGE_MASK (~((1UL << PAGE_SHIFT) - 1)) +#define PAGE_OFFSET (0xC0000000) +#define PAGE_PDE_CNT 1024 +#define PAGE_PTE_CNT 1024 #ifndef ASM #include #include -#define get_npd(addr) (((u32)(addr))>>22) -#define get_npt(addr) ((((u32)(addr))>>12) & 0x3FF) - +#define get_npd(addr) (((u32)(addr)) >> 22) +#define get_npt(addr) ((((u32)(addr)) >> 12) & 0x3FF) #include typedef unsigned long pde_t; typedef unsigned long pte_t; -#define PDECNT_PER_PAGE (PAGE_SIZE/sizeof(pde_t)) -#define PTECNT_PER_PAGE (PAGE_SIZE/sizeof(pte_t)) +#define PDECNT_PER_PAGE (PAGE_SIZE / sizeof(pde_t)) +#define PTECNT_PER_PAGE (PAGE_SIZE / sizeof(pte_t)) -#define PAGE_ITEMS (PAGE_SIZE/sizeof(unsigned long)) -#define PAGE_ALIGN(page) (((unsigned long)(page)) & PAGE_MASK) -#define PAGE_UP(page) (((unsigned long)page + PAGE_SIZE -1) & PAGE_MASK) -#define PAGE_DOWN PAGE_ALIGN +#define PAGE_ITEMS (PAGE_SIZE / sizeof(unsigned long)) +#define PAGE_ALIGN(page) (((unsigned long)(page)) & PAGE_MASK) +#define PAGE_UP(page) (((unsigned long)page + PAGE_SIZE - 1) & PAGE_MASK) +#define PAGE_DOWN PAGE_ALIGN -#define PAGE_FLAGS(addr) ((addr) - PAGE_ALIGN(addr)) +#define PAGE_FLAGS(addr) ((addr)-PAGE_ALIGN(addr)) #define va2pa(x) (((unsigned long)(x)) - PAGE_OFFSET) -#define pa2va(x) ((void *) (((unsigned long)(x)) + PAGE_OFFSET)) +#define pa2va(x) ((void *)(((unsigned long)(x)) + PAGE_OFFSET)) // pfn: page frame number -#define pa2pfn(addr) ((addr)>>PAGE_SHIFT) -#define pfn2pa(pfn) ((pfn)<> PAGE_SHIFT) +#define pfn2pa(pfn) ((pfn) << PAGE_SHIFT) -#define valid_va(addr) ((addr) >= PAGE_OFFSET) +#define va2pfn(addr) pa2pfn(va2pa(addr)) +#define pfn2va(pfn) pa2va(pfn2pa(pfn)) -#define PFN_UP(addr) (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT) -#define PFN_DW(addr) ((addr) >> PAGE_SHIFT) +#define valid_va(addr) ((addr) >= PAGE_OFFSET) -#define MAX_ORDER (11) +#define PFN_UP(addr) (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT) +#define PFN_DW(addr) ((addr) >> PAGE_SHIFT) -#define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) +#define MAX_ORDER (11) +#define LOAD_CR3(pde) asm("movl %%edx, %%cr3" ::"d"(va2pa(pde))) typedef unsigned int gfp_t; -enum page_flags { +enum page_flags +{ PG_Private, }; - + struct kmem_cache; typedef struct kmem_cache kmem_cache_t; @@ -85,12 +84,12 @@ typedef struct page unsigned long flags; unsigned long private; unsigned long index; - list_head_t lru; + list_head_t lru; - struct page *head_page; + struct page *head_page; unsigned int order; - void **freelist; // for slub + void **freelist; // for slub kmem_cache_t *cache; unsigned long inuse; @@ -103,33 +102,39 @@ page_t *_pa2page(unsigned long addr); #define va2page(addr) _va2page(PAGE_ALIGN(addr)) #define pa2page(addr) _pa2page(PAGE_ALIGN(addr)) -static inline page_t *get_head_page(page_t *page) { return page->head_page; } - -#define __GETPAGEFLAG(name) \ - static inline int Page##name(page_t *page) \ - {return constant_test_bit(PG_##name, &page->flags); } - -#define __SETPAGEFLAG(name) \ - static inline int SetPage##name(page_t *page) \ - {return test_and_set_bit(PG_##name, &page->flags); } - -#define __CLEARPAGEFLAG(name) \ - static inline int ClearPage##name(page_t *page) \ - {return test_and_clear_bit(PG_##name, &page->flags); } - +static inline page_t *get_head_page(page_t *page) +{ + return page->head_page; +} + +#define __GETPAGEFLAG(name) \ + static inline int Page##name(page_t *page) \ + { \ + return constant_test_bit(PG_##name, &page->flags); \ + } + +#define __SETPAGEFLAG(name) \ + static inline int SetPage##name(page_t *page) \ + { \ + return test_and_set_bit(PG_##name, &page->flags); \ + } + +#define __CLEARPAGEFLAG(name) \ + static inline int ClearPage##name(page_t *page) \ + { \ + return test_and_clear_bit(PG_##name, &page->flags); \ + } __GETPAGEFLAG(Private) __SETPAGEFLAG(Private) __CLEARPAGEFLAG(Private) - typedef struct free_area { unsigned long free_count; - list_head_t free_list; + list_head_t free_list; } free_area_t; - unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order); void free_pages(unsigned long addr); @@ -153,8 +158,6 @@ struct kmem_cache list_head_t list; }; - - // TODO Remove typedef struct page_ { @@ -174,13 +177,11 @@ typedef struct free_area_ unsigned int count; } FreeArea, *pFreeArea; - -pPage old_alloc_pages(unsigned int order); -void old_free_pages(pPage page); +pPage old_alloc_pages(unsigned int order); +void old_free_pages(pPage page); //void free_pages(pPage page, unsigned int order); -void disp_free_area(); - +void disp_free_area(); -#endif // ASM +#endif // ASM #endif //_PAGE_H diff --git a/include/pci.h b/include/pci.h index 0f086c7..4c48b32 100644 --- a/include/pci.h +++ b/include/pci.h @@ -18,8 +18,8 @@ #include -#define PCI_ADDR 0xCF8 // CONFIG_ADDRESS -#define PCI_DATA 0xCFC // CONFIG_DATA +#define PCI_ADDR 0xCF8 // CONFIG_ADDRESS +#define PCI_DATA 0xCFC // CONFIG_DATA // PCI Device /* @@ -66,10 +66,9 @@ extern list_head_t pci_devs; typedef struct pci_device { - list_head_t list; + list_head_t list; unsigned int bus, dev, devfn; - unsigned int vendor; unsigned int device; unsigned int command; @@ -85,11 +84,10 @@ typedef struct pci_device unsigned int intr_line; unsigned int intr_pin; - unsigned int primary_bus_nr; /* only for pci bridge */ + unsigned int primary_bus_nr; /* only for pci bridge */ unsigned int secondary_bus_nr; } __attribute__((packed)) pci_device_t; - #if 0 typedef union pci_device { @@ -114,78 +112,78 @@ typedef union pci_device } __attribute__((packed)) pci_device_t; #endif -#define PCI_VENDORID 0x00 -#define PCI_DEVICEID 0x02 -#define PCI_COMMAND 0x04 - #define PCI_COMMAND_IO 0x01 - #define PCI_COMMAND_MEMORY 0x02 - #define PCI_COMMAND_MASTER 0x04 - #define PCI_COMMAND_SPECIAL 0x08 - #define PCI_COMMAND_INVALIDATE 0x10 - #define PCI_COMMAND_VGA_PALETTE 0x20 - #define PCI_COMMAND_PARITY 0x40 - #define PCI_COMMAND_WAIT 0x80 - #define PCI_COMMAND_SERR 0x100 - #define PCI_COMMAND_FAST_BACK 0x200 - #define PCI_COMMAND_INTR_DISABLE 0x400 -#define PCI_STATUS 0x06 -#define PCI_REVISION 0x08 -#define PCI_PROGIF 0x09 -#define PCI_CLASSCODE 0x0A -#define PCI_HDRTYPE 0x0E - #define PCI_HDRTYPE_MASK 0x7F - #define PCI_HDRTYPE_NORMAL 0x00 - #define PCI_HDRTYPE_BRIDGE 0x01 /* PCI-to-PCI Bridge */ - #define PCI_HDRTYPE_CARDBUS 0x02 /* CardBus Bridge */ -#define PCI_BAR0 0x10 -#define PCI_BAR1 0x14 -#define PCI_BAR2 0x18 -#define PCI_BAR3 0x1C -#define PCI_BAR4 0x20 -#define PCI_BAR5 0x24 -#define PCI_PRIMARY_BUS_NUMBER 0x18 -#define PCI_SECONDARY_BUS_NUMBER 0x19 -#define PCI_INTRLINE 0x3C -#define PCI_INTRPIN 0x3D -#define PCI_MINGNT 0x3E -#define PCI_MAXLAT 0x3F +#define PCI_VENDORID 0x00 +#define PCI_DEVICEID 0x02 +#define PCI_COMMAND 0x04 +#define PCI_COMMAND_IO 0x01 +#define PCI_COMMAND_MEMORY 0x02 +#define PCI_COMMAND_MASTER 0x04 +#define PCI_COMMAND_SPECIAL 0x08 +#define PCI_COMMAND_INVALIDATE 0x10 +#define PCI_COMMAND_VGA_PALETTE 0x20 +#define PCI_COMMAND_PARITY 0x40 +#define PCI_COMMAND_WAIT 0x80 +#define PCI_COMMAND_SERR 0x100 +#define PCI_COMMAND_FAST_BACK 0x200 +#define PCI_COMMAND_INTR_DISABLE 0x400 +#define PCI_STATUS 0x06 +#define PCI_REVISION 0x08 +#define PCI_PROGIF 0x09 +#define PCI_CLASSCODE 0x0A +#define PCI_HDRTYPE 0x0E +#define PCI_HDRTYPE_MASK 0x7F +#define PCI_HDRTYPE_NORMAL 0x00 +#define PCI_HDRTYPE_BRIDGE 0x01 /* PCI-to-PCI Bridge */ +#define PCI_HDRTYPE_CARDBUS 0x02 /* CardBus Bridge */ +#define PCI_BAR0 0x10 +#define PCI_BAR1 0x14 +#define PCI_BAR2 0x18 +#define PCI_BAR3 0x1C +#define PCI_BAR4 0x20 +#define PCI_BAR5 0x24 +#define PCI_PRIMARY_BUS_NUMBER 0x18 +#define PCI_SECONDARY_BUS_NUMBER 0x19 +#define PCI_INTRLINE 0x3C +#define PCI_INTRPIN 0x3D +#define PCI_MINGNT 0x3E +#define PCI_MAXLAT 0x3F // PCI Command Register -#define PCI_CMD(bus, dev, devfn, reg) (0x80000000 | (bus << 16) | (dev << 11) | (devfn << 8) | (reg/* & 0xFC */)) +#define PCI_CMD(bus, dev, devfn, reg) (0x80000000 | (bus << 16) | (dev << 11) | (devfn << 8) | (reg /* & 0xFC */)) #define PCI_CONFIG_CMD(cmd) (cmd & ~3) #define PCI_GET_CMD_REG(cmd) (cmd & 0xFF) - /* PCI IDS */ // Display -#define PCI_BASE_CLASS_DISPLAY 0x03 -#define PCI_CLASS_DISPLAY_VGA 0x0300 +#define PCI_BASE_CLASS_DISPLAY 0x03 +#define PCI_CLASS_DISPLAY_VGA 0x0300 // Bridge -#define PCI_BASE_CLASS_BRIDGE 0x06 -#define PCI_CLASS_BRIDGE_HOST 0x0600 -#define PCI_CLASS_BRIDGE_ISA 0x0601 -#define PCI_CLASS_BRIDGE_PCI 0x0604 -#define PCI_CLASS_BRIDGE_CARDBUS 0x0607 +#define PCI_BASE_CLASS_BRIDGE 0x06 +#define PCI_CLASS_BRIDGE_HOST 0x0600 +#define PCI_CLASS_BRIDGE_ISA 0x0601 +#define PCI_CLASS_BRIDGE_PCI 0x0604 +#define PCI_CLASS_BRIDGE_CARDBUS 0x0607 /* Vendors*/ -#define PCI_VENDORID_COMPAQ 0x0E11 -#define PCI_VENDORID_INTEL 0x8086 -#define PCI_VENDORID_ATI 0x1002 -#define PCI_VENDORID_IBM 0x1014 -#define PCI_VENDORID_AMD 0x1022 -#define PCI_VENDORID_HP 0x103C -#define PCI_VENDORID_SONY 0x104D -#define PCI_VENDORID_MOTOROLA 0x1057 -#define PCI_VENDORID_APPLE 0x106B -#define PCI_VENDORID_SUN 0x108E -#define PCI_VENDORID_NVIDIA 0x10DE -#define PCI_VENDORID_REALTEK 0x10EC +#define PCI_VENDORID_COMPAQ 0x0E11 +#define PCI_VENDORID_INTEL 0x8086 +#define PCI_VENDORID_ATI 0x1002 +#define PCI_VENDORID_IBM 0x1014 +#define PCI_VENDORID_AMD 0x1022 +#define PCI_VENDORID_HP 0x103C +#define PCI_VENDORID_SONY 0x104D +#define PCI_VENDORID_MOTOROLA 0x1057 +#define PCI_VENDORID_APPLE 0x106B +#define PCI_VENDORID_SUN 0x108E +#define PCI_VENDORID_NVIDIA 0x10DE +#define PCI_VENDORID_REALTEK 0x10EC pci_device_t *pci_find_device(unsigned int vendor, unsigned int device); pci_device_t *pci_find_device_by_classcode(unsigned int classcode); -static inline u32 pci_cmd(pci_device_t *pci, unsigned int reg) { +static inline u32 pci_cmd(pci_device_t *pci, unsigned int reg) +{ return PCI_CMD(pci->bus, pci->dev, pci->devfn, reg); } @@ -196,9 +194,6 @@ void pci_write_config_byte(int value, int cmd); void pci_write_config_word(int value, int cmd); void pci_write_config_long(int value, int cmd); - - - // PCI Bridge /* * 31 16 15 0 diff --git a/include/printk.h b/include/printk.h index 07717c0..dc4ed3c 100644 --- a/include/printk.h +++ b/include/printk.h @@ -21,12 +21,13 @@ int printk(const char *fmtstr, ...); int printd(const char *fmtstr, ...); int printlo(unsigned int line, unsigned int offset, const char *fmtstr, ...); -#define printl(line, fmt, args...) printlo(line, 1, fmt, ## args) -#define printll(line, fmt, args...) printlo(line, 0, fmt, ## args) -#define printlr(line, fmt, args...) printlo(line, 40, fmt, ## args) +#define printl(line, fmt, args...) printlo(line, 1, fmt, ##args) +#define printll(line, fmt, args...) printlo(line, 0, fmt, ##args) +#define printlr(line, fmt, args...) printlo(line, 40, fmt, ##args) // monitor print line -enum { +enum +{ MPL_TITLE, MPL_ROOTDEV, MPL_CLOCK, @@ -47,4 +48,3 @@ enum { MPL_TASK_8, MPL_END }; - diff --git a/include/processor.h b/include/processor.h index 3661e67..033f403 100644 --- a/include/processor.h +++ b/include/processor.h @@ -14,58 +14,54 @@ *-------------------------------------------------------------------------- */ - - -#ifndef _DESCRIPTOR_H -#define _DESCRIPTOR_H +#ifndef _DESCRIPTOR_H +#define _DESCRIPTOR_H #include "types.h" #include "system.h" #include "string.h" //Descriptor Attrib. -#define DA_E 0x8 //Executable -#define DA_ED 0x4 //For Data SEG -#define DA_C 0x4 //For Code SEG -#define DA_W 0x2 //For Data SEG -#define DA_A 0x1 //Access. 1-Accessed. +#define DA_E 0x8 //Executable +#define DA_ED 0x4 //For Data SEG +#define DA_C 0x4 //For Code SEG +#define DA_W 0x2 //For Data SEG +#define DA_A 0x1 //Access. 1-Accessed. //we just need Accessed. //For Data Segment. -#define DSA_R (DA_A) -#define DSA_RW (DA_A | DA_W) -#define DSA_RD (DA_A | DA_ED) //down to ... -#define DSA_RWD (DA_A | DA_W | DA_ED) +#define DSA_R (DA_A) +#define DSA_RW (DA_A | DA_W) +#define DSA_RD (DA_A | DA_ED) //down to ... +#define DSA_RWD (DA_A | DA_W | DA_ED) //For Code Segment. -#define CSA_E (DA_E | DA_A) -#define CSA_ER (DA_E | DA_A | DA_R) -#define CSA_EC (DA_E | DA_A | DA_C) -#define CSA_ERC (DA_E | DA_A | DA_R | DA_C) - +#define CSA_E (DA_E | DA_A) +#define CSA_ER (DA_E | DA_A | DA_R) +#define CSA_EC (DA_E | DA_A | DA_C) +#define CSA_ERC (DA_E | DA_A | DA_R | DA_C) //------------------------------------------------------------------------- // Code And Data Descriptor. // Program Segment Descriptor. //------------------------------------------------------------------------- -typedef struct +typedef struct { unsigned short limitL; unsigned short baseL; unsigned char baseM; - unsigned char type:4; - unsigned char S:1; //allways set to 0. NOT SYS SEG DESC - unsigned char DPL:2; - unsigned char P:1; //allways set to 1. - - unsigned char limitH:4; - unsigned char AVL:1; - unsigned char zero:1; //should be set to 0 - unsigned char DB:1; //0--16bits,1--32bits - unsigned char G:1; //set to 1. We just need 4K size. + unsigned char type : 4; + unsigned char S : 1; //allways set to 0. NOT SYS SEG DESC + unsigned char DPL : 2; + unsigned char P : 1; //allways set to 1. + + unsigned char limitH : 4; + unsigned char AVL : 1; + unsigned char zero : 1; //should be set to 0 + unsigned char DB : 1; //0--16bits,1--32bits + unsigned char G : 1; //set to 1. We just need 4K size. unsigned char baseH; -} Seg,*pSeg; - +} Seg, *pSeg; //------------------------------------------------------------------------- // TSS State,LDT @@ -73,17 +69,17 @@ typedef struct //------------------------------------------------------------------------- typedef struct { - unsigned short eaddrL; + unsigned short eaddrL; - unsigned short selector; + unsigned short selector; - unsigned char parmeter:5; //for call gate - //reserved by other gates. - unsigned char zero:3; - unsigned char type:4; - unsigned char S:1; - unsigned char DPL:2; - unsigned char P:1; + unsigned char parmeter : 5; //for call gate + //reserved by other gates. + unsigned char zero : 3; + unsigned char type : 4; + unsigned char S : 1; + unsigned char DPL : 2; + unsigned char P : 1; unsigned short eaddrH; @@ -92,116 +88,115 @@ typedef struct //just used for type... typedef union { - Seg seg; - Gate gate; + Seg seg; + Gate gate; } Desc, *pDesc; - -#define NGDT 256 -#define NIDT 256 -#define NLDT 5 -extern Desc idt[NIDT]; -extern Desc gdt[NGDT]; +#define NGDT 256 +#define NIDT 256 +#define NLDT 5 +extern Desc idt[NIDT]; +extern Desc gdt[NGDT]; //------------------------------------------------------------------------- //Define Gate Types... //------------------------------------------------------------------------- -#define INTR_GATE 0x0E //Clear 'IF' bit.---->Close Interrupt -#define TRAP_GATE 0x0F //Keep 'IF' bit. -#define TSS_DESC 0x09 +#define INTR_GATE 0x0E //Clear 'IF' bit.---->Close Interrupt +#define TRAP_GATE 0x0F //Keep 'IF' bit. +#define TSS_DESC 0x09 static inline void _init_desc(pDesc desc) { - if(0xc010a1c8 == (unsigned long)desc) + if (0xc010a1c8 == (unsigned long)desc) asm("xchg %bx,%bx"); memset((char *)desc, 0, sizeof(Desc)); } - static inline Desc _create_seg(u8 type, u8 DPL) { - Desc d; - pSeg p=&d.seg; + Desc d; + pSeg p = &d.seg; _init_desc(&d); - p->limitL = 0xFFFF; - p->limitH = 0x0F; - p->type = type; - p->S = 0x1; - p->DPL = DPL; - p->P = 0x1; - p->G = 0x1; - p->DB = 0x1; + p->limitL = 0xFFFF; + p->limitH = 0x0F; + p->type = type; + p->S = 0x1; + p->DPL = DPL; + p->P = 0x1; + p->G = 0x1; + p->DB = 0x1; return d; } - - - - static inline Desc _create_gate(u32 handler, u8 type, u8 DPL) { - Desc d; - pGate p = &d.gate; + Desc d; + pGate p = &d.gate; _init_desc(&d); - p->eaddrL = 0xFFFF & handler; - p->eaddrH = 0xFFFF & (handler >> 16); - p->selector = SELECTOR_KRNL_CS; - p->type = type; - p->P = 0x1; - p->DPL = DPL; + p->eaddrL = 0xFFFF & handler; + p->eaddrH = 0xFFFF & (handler >> 16); + p->selector = SELECTOR_KRNL_CS; + p->type = type; + p->P = 0x1; + p->DPL = DPL; return d; } -static inline void set_idt_gate(u32 vec, u32 handler,u8 type, u8 DPL) +static inline void set_idt_gate(u32 vec, u32 handler, u8 type, u8 DPL) { idt[vec] = _create_gate(handler, type, DPL); } -#define set_sys_int(vect, type, DPL, handler) do { void handler(); set_idt_gate(vect, (u32)handler, type, DPL); } while(0) +#define set_sys_int(vect, type, DPL, handler) \ + do \ + { \ + void handler(); \ + set_idt_gate(vect, (u32)handler, type, DPL); \ + } while (0) -typedef struct +typedef struct { - u16 backlink, _backlink; - u32 esp0; - u16 ss0, _ss0; - u32 esp1; - u16 ss1, _ss1; - u32 esp2; - u16 ss2, _ss2; - u32 cr3; - u32 eip; - u32 eflags; - u32 eax; - u32 ecx; - u32 edx; - u32 ebx; - u32 esp; - u32 ebp; - u32 esi; - u32 edi; - u16 es, _es; - u16 cs, _cs; - u16 ss, _ss; - u16 ds, _ds; - u16 fs, _fs; - u16 gs, _gs; - u16 ldt, _ldt; - u16 T:1, _T:15, iomap_base; + u16 backlink, _backlink; + u32 esp0; + u16 ss0, _ss0; + u32 esp1; + u16 ss1, _ss1; + u32 esp2; + u16 ss2, _ss2; + u32 cr3; + u32 eip; + u32 eflags; + u32 eax; + u32 ecx; + u32 edx; + u32 ebx; + u32 esp; + u32 ebp; + u32 esi; + u32 edi; + u16 es, _es; + u16 cs, _cs; + u16 ss, _ss; + u16 ds, _ds; + u16 fs, _fs; + u16 gs, _gs; + u16 ldt, _ldt; + u16 T : 1, _T : 15, iomap_base; } TSS, *pTSS; static inline void set_tss_gate(u32 vec, u32 addr) { #if 1 - pSeg p = (pSeg) (gdt+vec); + pSeg p = (pSeg)(gdt + vec); _init_desc((pDesc)p); p->limitL = 0xFFFF & sizeof(TSS); - p->limitH = 0x0F & (sizeof(TSS) >> 16); + p->limitH = 0x0F & (sizeof(TSS) >> 16); p->baseL = 0xFFFF & addr; - p->baseM = 0xFF & (addr>>16); - p->baseH = 0xFF & (addr>>24); + p->baseM = 0xFF & (addr >> 16); + p->baseH = 0xFF & (addr >> 24); p->P = 1; p->DPL = PRIVILEGE_USER; @@ -211,8 +206,6 @@ static inline void set_tss_gate(u32 vec, u32 addr) #endif } +extern TSS tss; -extern TSS tss; - - -#endif//_DESCRIPTOR_H +#endif //_DESCRIPTOR_H diff --git a/include/semaphore.h b/include/semaphore.h index 8ea8d78..8f1343b 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -19,13 +19,13 @@ typedef struct semaphore list_head_t wait_list; } semaphore_t; -#define SEMAPHORE_INITIALIZER(name, n) \ -{ \ - .cnt = (n), \ - .wait_list = LIST_HEAD_INIT((name).wait_list) \ -} +#define SEMAPHORE_INITIALIZER(name, n) \ + { \ + .cnt = (n), \ + .wait_list = LIST_HEAD_INIT((name).wait_list) \ + } -#define DECLARE_MUTEX(name) \ +#define DECLARE_MUTEX(name) \ semaphore_t name = SEMAPHORE_INITIALIZER(name, 1) void down(semaphore_t *s); diff --git a/include/stat.h b/include/stat.h index 53a3823..2344166 100644 --- a/include/stat.h +++ b/include/stat.h @@ -10,49 +10,49 @@ *-------------------------------------------------------------------------- */ -#ifndef _STAT_H +#ifndef _STAT_H #define _STAT_H -typedef struct stat { - unsigned long st_dev; - unsigned long st_ino; +typedef struct stat +{ + unsigned long st_dev; + unsigned long st_ino; unsigned short st_mode; unsigned short st_nlink; unsigned short st_uid; unsigned short st_gid; - unsigned long st_rdev; - unsigned long st_size; - unsigned long st_blksize; - unsigned long st_blocks; - unsigned long st_atime; - unsigned long st_atime_nsec; - unsigned long st_mtime; - unsigned long st_mtime_nsec; - unsigned long st_ctime; - unsigned long st_ctime_nsec; - unsigned long __unused4; - unsigned long __unused5; + unsigned long st_rdev; + unsigned long st_size; + unsigned long st_blksize; + unsigned long st_blocks; + unsigned long st_atime; + unsigned long st_atime_nsec; + unsigned long st_mtime; + unsigned long st_mtime_nsec; + unsigned long st_ctime; + unsigned long st_ctime_nsec; + unsigned long __unused4; + unsigned long __unused5; } Stat, *pStat; -#define S_IFMT 00170000 +#define S_IFMT 00170000 #define S_IFSOCK 0140000 -#define S_IFLNK 0120000 -#define S_IFREG 0100000 -#define S_IFBLK 0060000 -#define S_IFDIR 0040000 -#define S_IFCHR 0020000 -#define S_IFIFO 0010000 -#define S_ISUID 0004000 -#define S_ISGID 0002000 -#define S_ISVTX 0001000 - -#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) -#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) -#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) -#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) -#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) +#define S_IFLNK 0120000 +#define S_IFREG 0100000 +#define S_IFBLK 0060000 +#define S_IFDIR 0040000 +#define S_IFCHR 0020000 +#define S_IFIFO 0010000 +#define S_ISUID 0004000 +#define S_ISGID 0002000 +#define S_ISVTX 0001000 +#define S_ISLNK(m) (((m)&S_IFMT) == S_IFLNK) +#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) +#define S_ISCHR(m) (((m)&S_IFMT) == S_IFCHR) +#define S_ISBLK(m) (((m)&S_IFMT) == S_IFBLK) +#define S_ISFIFO(m) (((m)&S_IFMT) == S_IFIFO) +#define S_ISSOCK(m) (((m)&S_IFMT) == S_IFSOCK) #endif //_STAT_H diff --git a/include/stdio.h b/include/stdio.h index ac83058..203b66d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -22,11 +22,11 @@ extern int write(int fd, const char *buf, unsigned long size); static inline int printf(const char *fmt, ...) { char ptfbuf[512]; - char *args = (char*)(((char*)&fmt)+4); + char *args = (char *)(((char *)&fmt) + 4); vsprintf(ptfbuf, fmt, args); //asm("xchg %bx,%bx;"); - + return write(0, ptfbuf, strlen(ptfbuf)); } diff --git a/include/stdlib.h b/include/stdlib.h index b405cf7..17f4008 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -10,9 +10,9 @@ *-------------------------------------------------------------------------- */ -#ifndef _STDLIB_H +#ifndef _STDLIB_H #define _STDLIB_H -extern int atoi(const char *s); +extern int atoi(const char *s); #endif //_STDLIB_H diff --git a/include/string.h b/include/string.h index b40d15c..2c7605b 100644 --- a/include/string.h +++ b/include/string.h @@ -18,14 +18,13 @@ #define _STRING_H #include "types.h" -char *strcpy(char *dest, const char *src); +char *strcpy(char *dest, const char *src); size_t strlen(const char *str); -int strcmp(const char *a, const char *b); -int strncmp(const char *a, const char *b, size_t count); -char *strcat(char *dest, const char *src); +int strcmp(const char *a, const char *b); +int strncmp(const char *a, const char *b, size_t count); +char *strcat(char *dest, const char *src); char *strstr(const char *a, const char *b); - void *memcpy(void *dest, const void *src, size_t size); void memset(void *dest, char ch, size_t size); int memcmp(const void *a, const void *b, size_t count); diff --git a/include/syscall.h b/include/syscall.h index a1f1067..5e8bd15 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -17,9 +17,9 @@ #ifndef _SYSCALL_H #define _SYSCALL_H -#define SYSC_NUM 256 +#define SYSC_NUM 256 -#ifndef ASM +#ifndef ASM #include "page.h" #include "errno.h" @@ -30,11 +30,11 @@ int _syscall2(int nr, unsigned long a, unsigned long b); int _syscall3(int nr, unsigned long a, unsigned long b, unsigned long c); int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d); -#define syscall0(nr) _syscall0(nr) -#define syscall1(nr, a) _syscall1(nr, (unsigned long)a) -#define syscall2(nr, a, b) _syscall2(nr, (unsigned long)a, (unsigned long)b) -#define syscall3(nr, a, b, c) _syscall3(nr, (unsigned long)a, (unsigned long)b, (unsigned long)c) -#define syscall4(nr, a, b, c, d) _syscall4(nr, (unsigned long)a, (unsigned long)b, (unsigned long)c, (unsigned long)d) +#define syscall0(nr) _syscall0(nr) +#define syscall1(nr, a) _syscall1(nr, (unsigned long)a) +#define syscall2(nr, a, b) _syscall2(nr, (unsigned long)a, (unsigned long)b) +#define syscall3(nr, a, b, c) _syscall3(nr, (unsigned long)a, (unsigned long)b, (unsigned long)c) +#define syscall4(nr, a, b, c, d) _syscall4(nr, (unsigned long)a, (unsigned long)b, (unsigned long)c, (unsigned long)d) enum { @@ -53,6 +53,6 @@ enum SYSC_DEBUG }; -#endif // ASM +#endif // ASM #endif //_SYSCALL_H diff --git a/include/system.h b/include/system.h index ea8dbf6..24f16ff 100644 --- a/include/system.h +++ b/include/system.h @@ -19,76 +19,79 @@ #include #include -#define KRNLADDR PAGE_OFFSET - -#define PT_REGS_EBX 0 -#define PT_REGS_ECX 4 -#define PT_REGS_EDX 8 -#define PT_REGS_ESI 12 -#define PT_REGS_EDI 16 -#define PT_REGS_EBP 20 -#define PT_REGS_EAX 24 -#define PT_REGS_DS 28 -#define PT_REGS_ES 32 -#define PT_REGS_FS 36 -#define PT_REGS_GS 40 -#define PT_REGS_IRQ 44 -#define PT_REGS_EIP 48 -#define PT_REGS_CS 52 -#define PT_REGS_EFLAGS 56 -#define PT_REGS_ESP 60 -#define PT_REGS_SS 64 +#define KRNLADDR PAGE_OFFSET + +#define PT_REGS_EBX 0 +#define PT_REGS_ECX 4 +#define PT_REGS_EDX 8 +#define PT_REGS_ESI 12 +#define PT_REGS_EDI 16 +#define PT_REGS_EBP 20 +#define PT_REGS_EAX 24 +#define PT_REGS_DS 28 +#define PT_REGS_ES 32 +#define PT_REGS_FS 36 +#define PT_REGS_GS 40 +#define PT_REGS_IRQ 44 +#define PT_REGS_EIP 48 +#define PT_REGS_CS 52 +#define PT_REGS_EFLAGS 56 +#define PT_REGS_ESP 60 +#define PT_REGS_SS 64 #ifndef ASM #include "types.h" #include "printk.h" -#define likely(x) __builtin_expect(!!(x), 1) -#define unlikely(x) __builtin_expect(!!(x), 0) - -#define typecheck(type, x) \ -({ type __dummy; \ - typeof(x) __dummy2; \ - (void)(&__dummy == &__dummy2); \ - 1; \ -}) - -void *kmalloc(size_t size, gfp_t gfpflags); -void kfree(void *addr); - -#define panic(msg, ...) do { \ - asm("cli;"); \ - printk("PANIC:" \ - msg \ - " file:%s function:%s line:%d\n", \ - ##__VA_ARGS__, __FILE__, __FUNCTION__, __LINE__); \ - while(1); \ -} while(0); +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + +#define typecheck(type, x) \ + ( \ + { \ + type __dummy; \ + typeof(x) __dummy2; \ + (void)(&__dummy == &__dummy2); \ + 1; \ + }) + +void *kmalloc(size_t size, gfp_t gfpflags); +void kfree(void *addr); + +#define panic(msg, ...) \ + do \ + { \ + asm("cli;"); \ + printk("PANIC:" msg \ + " file:%s function:%s line:%d\n", \ + ##__VA_ARGS__, __FILE__, __FUNCTION__, __LINE__); \ + while (1) \ + ; \ + } while (0); extern char etext, edata, end; -extern char gdtr[6],idtr[6]; -#define lgdt() __asm__ __volatile__("lgdt gdtr") -#define sgdt() __asm__ __volatile__("sgdt gdtr") -#define lidt() __asm__ __volatile__("lidt idtr") -#define sidt() __asm__ __volatile__("sidt idtr") +extern char gdtr[6], idtr[6]; +#define lgdt() __asm__ __volatile__("lgdt gdtr") +#define sgdt() __asm__ __volatile__("sgdt gdtr") +#define lidt() __asm__ __volatile__("lidt idtr") +#define sidt() __asm__ __volatile__("sidt idtr") -#define cli() __asm__ __volatile__("cli") -#define sti() __asm__ __volatile__("sti") -#define disableIRQ() cli() -#define enableIRQ() sti() +#define cli() __asm__ __volatile__("cli") +#define sti() __asm__ __volatile__("sti") +#define disableIRQ() cli() +#define enableIRQ() sti() -#define ALIGN(x, a) (((x)+(a)-1) & ~((a)-1)) +#define ALIGN(x, a) (((x) + (a)-1) & ~((a)-1)) // 1 GB -#define MAX_SUPT_PHYMM_SIZE (1UL<<30) - -#define INT_STACK_SIZE PAGE_SIZE +#define MAX_SUPT_PHYMM_SIZE (1UL << 30) +#define INT_STACK_SIZE PAGE_SIZE enum GDTSelectorIndex { - INDEX_SPACE=0, + INDEX_SPACE = 0, INDEX_KCODE, INDEX_KDATA, INDEX_UCODE, @@ -106,27 +109,27 @@ enum GDTSelectorIndex typedef struct pt_regs { - u32 ebx; - u32 ecx; - u32 edx; - u32 esi; - u32 edi; - u32 ebp; - u32 eax; - u16 ds, _ds; - u16 es, _es; - u16 fs, _fs; - u16 gs, _gs; + u32 ebx; + u32 ecx; + u32 edx; + u32 esi; + u32 edi; + u32 ebp; + u32 eax; + u16 ds, _ds; + u16 es, _es; + u16 fs, _fs; + u16 gs, _gs; union { - u32 irq; - u32 errcode; + u32 irq; + u32 errcode; }; - u32 eip; - u16 cs, _cs; - u32 eflags; - u32 esp; - u16 ss, _ss; + u32 eip; + u16 cs, _cs; + u32 eflags; + u32 esp; + u16 ss, _ss; } __attribute__((packed)) pt_regs_t; typedef unsigned long dev_t; @@ -134,15 +137,15 @@ typedef unsigned long dev_t; typedef struct system { u32 mmap_addr; - u32 mmap_size; // Byte + u32 mmap_size; // Byte - u32 mm_lower; // KB - u32 mm_upper; // KB - u64 mm_size; // Byte + u32 mm_lower; // KB + u32 mm_upper; // KB + u64 mm_size; // Byte u32 page_count; pPage page_map; - u32 page_bitmap; + u32 page_bitmap; u32 *page_dir; u32 *pte_start; @@ -159,7 +162,7 @@ typedef struct system u32 boot_device; dev_t root_dev; -#define CMD_LINE_SIZE 128 +#define CMD_LINE_SIZE 128 const char *cmdline; u32 debug; @@ -167,8 +170,7 @@ typedef struct system u32 delay; } System, *pSystem; - -extern System system; +extern System system; void system_delay(); @@ -176,73 +178,71 @@ void system_delay(); #endif -#define SAVE_REGS \ - cld; \ - pushl %gs; \ - pushl %fs; \ - pushl %es; \ - pushl %ds; \ - pushl %eax; \ - pushl %ebp; \ - pushl %esi; \ - pushl %edi; \ - pushl %edx; \ - pushl %ecx; \ - pushl %ebx; - -#define RESTORE_REGS \ - popl %ebx; \ - popl %ecx; \ - popl %edx; \ - popl %edi; \ - popl %esi; \ - popl %ebp; \ - popl %eax; \ - popl %ds; \ - popl %es; \ - popl %fs; \ - popl %gs; - - -#define PRIVILEGE_KRNL 0x0 -#define PRIVILEGE_USER 0x3 - -#define INDEX_UCODE 3 -#define INDEX_UDATA 4 +#define SAVE_REGS \ + cld; \ + pushl % gs; \ + pushl % fs; \ + pushl % es; \ + pushl % ds; \ + pushl % eax; \ + pushl % ebp; \ + pushl % esi; \ + pushl % edi; \ + pushl % edx; \ + pushl % ecx; \ + pushl % ebx; + +#define RESTORE_REGS \ + popl % ebx; \ + popl % ecx; \ + popl % edx; \ + popl % edi; \ + popl % esi; \ + popl % ebp; \ + popl % eax; \ + popl % ds; \ + popl % es; \ + popl % fs; \ + popl % gs; + +#define PRIVILEGE_KRNL 0x0 +#define PRIVILEGE_USER 0x3 + +#define INDEX_UCODE 3 +#define INDEX_UDATA 4 /* *8 == <<3 . but <<3 is not right for asm code. */ -#define SELECTOR_KRNL_CS (INDEX_KCODE*8) -#define SELECTOR_KRNL_DS (INDEX_KDATA*8) -#define SELECTOR_KRNL_SS SELECTOR_KRNL_DS -#define SELECTOR_USER_CS ((INDEX_UCODE*8)|PRIVILEGE_USER) -#define SELECTOR_USER_DS ((INDEX_UDATA*8)|PRIVILEGE_USER) -#define SELECTOR_USER_SS SELECTOR_USER_DS - +#define SELECTOR_KRNL_CS (INDEX_KCODE * 8) +#define SELECTOR_KRNL_DS (INDEX_KDATA * 8) +#define SELECTOR_KRNL_SS SELECTOR_KRNL_DS +#define SELECTOR_USER_CS ((INDEX_UCODE * 8) | PRIVILEGE_USER) +#define SELECTOR_USER_DS ((INDEX_UDATA * 8) | PRIVILEGE_USER) +#define SELECTOR_USER_SS SELECTOR_USER_DS #if 0 -#define INT_VECT_DIVIDE 0x0 -#define INT_VECT_DEBUG 0x1 -#define INT_VECT_NMI 0x2 -#define INT_VECT_BREAKPOINT 0x3 /* Break Point */ -#define INT_VECT_OVERFLOW 0x4 -#define INT_VECT_BOUNDS 0x5 -#define INT_VECT_INVALOP 0x6 -#define INT_VECT_COPROCNOT 0x7 -#define INT_VECT_DOUBLEFAULT 0x8 /* Double Fault */ -#define INT_VECT_COPROCSEG 0x9 -#define INT_VECT_INVALTSS 0xA -#define INT_VECT_SEGNOT 0xB -#define INT_VECT_STACKFAULT 0xC /* Stack Fault */ -#define INT_VECT_PROTECTION 0xD -#define INT_VECT_PAGEFAULT 0xE -#define INT_VECT_COPROCERR 0x10 +#define INT_VECT_DIVIDE 0x0 +#define INT_VECT_DEBUG 0x1 +#define INT_VECT_NMI 0x2 +#define INT_VECT_BREAKPOINT 0x3 /* Break Point */ +#define INT_VECT_OVERFLOW 0x4 +#define INT_VECT_BOUNDS 0x5 +#define INT_VECT_INVALOP 0x6 +#define INT_VECT_COPROCNOT 0x7 +#define INT_VECT_DOUBLEFAULT 0x8 /* Double Fault */ +#define INT_VECT_COPROCSEG 0x9 +#define INT_VECT_INVALTSS 0xA +#define INT_VECT_SEGNOT 0xB +#define INT_VECT_STACKFAULT 0xC /* Stack Fault */ +#define INT_VECT_PROTECTION 0xD +#define INT_VECT_PAGEFAULT 0xE +#define INT_VECT_COPROCERR 0x10 #endif -#define INT_VECT_IRQ0 0x20 -#define INT_VECT_IRQ8 0x28 +#define INT_VECT_IRQ0 0x20 +#define INT_VECT_IRQ8 0x28 -#define REBOOT_RESTART 0x00 -#define REBOOT_POWEROFF 0x01 +#define REBOOT_RESTART 0x00 +#define REBOOT_POWEROFF 0x01 -#define KRNL_INIT_STACK_SIZE 4096 +#define KRNL_INIT_STACK_SIZE 4096 #endif //_SYSTEM_H diff --git a/include/task.h b/include/task.h index 8cfa109..a29f682 100644 --- a/include/task.h +++ b/include/task.h @@ -15,7 +15,7 @@ #define TASK_SIZE 4096 -#define TI_preempt_cnt 0 +#define TI_preempt_cnt 0 #ifndef ASM #include @@ -34,21 +34,20 @@ enum TASK_END, }; -#define TASK_NAME_SIZE 32 +#define TASK_NAME_SIZE 32 typedef struct wait_queue_head { list_head_t task_list; } wait_queue_head_t; - typedef union task_union { struct { unsigned long preempt_cnt; - unsigned long esp0; /* kernel stack */ + unsigned long esp0; /* kernel stack */ /* for context switch */ unsigned long esp; @@ -70,7 +69,7 @@ typedef union task_union wait_queue_head_t wait; - unsigned int cnt; // debug only + unsigned int cnt; // debug only }; unsigned char stack[TASK_SIZE]; @@ -81,7 +80,9 @@ task_union *alloc_task_union(); static inline task_union *get_current() { task_union *tsk; - asm("andl %%esp, %0;":"=r"(tsk):"0"(~(TASK_SIZE-1))); + asm("andl %%esp, %0;" + : "=r"(tsk) + : "0"(~(TASK_SIZE - 1))); return tsk; } @@ -94,12 +95,12 @@ static inline pid_t sysc_getpid() task_union *find_task(pid_t pid); -#define ROOT_TSK_PID (0) +#define ROOT_TSK_PID (0) #define TASK_INIT_WEIGHT 0 -#define get_tsk_from_list(p) list_entry((p), Task, list) -#define del_tsk_from_list(tsk) list_del((&tsk->list)) +#define get_tsk_from_list(p) list_entry((p), Task, list) +#define del_tsk_from_list(tsk) list_del((&tsk->list)) #endif #endif //_TASK_H diff --git a/include/types.h b/include/types.h index 769a273..a07163d 100644 --- a/include/types.h +++ b/include/types.h @@ -17,42 +17,45 @@ #ifndef _TYPES_H #define _TYPES_H -typedef unsigned int size_t; -typedef int ssize_t; - -typedef signed char s8; -typedef signed short s16; -typedef signed long s32; -typedef signed long long s64; - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; -typedef unsigned long u32; -typedef unsigned long long u64; - -typedef signed char s8_t; -typedef signed short s16_t; -typedef signed long s32_t; -typedef signed long long s64_t; - -typedef unsigned char u8_t; -typedef unsigned short u16_t; -typedef unsigned long u32_t; -typedef unsigned long u32_t; -typedef unsigned long long u64_t; - -typedef unsigned long pid_t; -typedef unsigned long mode_t; - - -#define NULL ((void*)0) - -typedef enum { false, true } bool; +typedef unsigned int size_t; +typedef int ssize_t; + +typedef signed char s8; +typedef signed short s16; +typedef signed long s32; +typedef signed long long s64; + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; +typedef unsigned long u32; +typedef unsigned long long u64; + +typedef signed char s8_t; +typedef signed short s16_t; +typedef signed long s32_t; +typedef signed long long s64_t; + +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned long u32_t; +typedef unsigned long u32_t; +typedef unsigned long long u64_t; + +typedef unsigned long pid_t; +typedef unsigned long mode_t; + +#define NULL ((void *)0) + +typedef enum +{ + false, + true +} bool; //========================================================================= //Define kinds of function's type ... //========================================================================= -typedef void (*pf_intr)(); +typedef void (*pf_intr)(); #endif //_TYPES_H diff --git a/include/unistd.h b/include/unistd.h index 692326c..08119b2 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -10,12 +10,12 @@ *-------------------------------------------------------------------------- */ -#ifndef _UNISTD_H +#ifndef _UNISTD_H #define _UNISTD_H #include -extern pid_t fork(); -extern int exit(int status); +extern pid_t fork(); +extern int exit(int status); #endif //_UNISTD_H diff --git a/include/wait.h b/include/wait.h index ebcc8a2..78af596 100644 --- a/include/wait.h +++ b/include/wait.h @@ -22,24 +22,23 @@ typedef struct list_head_t task_list; } wait_queue_t; -#define WAIT_QUEUE_HEAD_INITIALIZER(name) \ -{ \ - .task_list = LIST_HEAD_INIT((name).task_list) \ -} +#define WAIT_QUEUE_HEAD_INITIALIZER(name) \ + { \ + .task_list = LIST_HEAD_INIT((name).task_list) \ + } -#define DECLARE_WAIT_QUEUE_HEAD(name) \ +#define DECLARE_WAIT_QUEUE_HEAD(name) \ wait_queue_head_t name = WAIT_QUEUE_HEAD_INITIALIZER(name) -#define WAIT_QUEUE_INITIALIZER(name, tsk) \ -{ \ - .task = tsk, \ - .task_list = LIST_HEAD_INIT((name).task_list) \ -} +#define WAIT_QUEUE_INITIALIZER(name, tsk) \ + { \ + .task = tsk, \ + .task_list = LIST_HEAD_INIT((name).task_list) \ + } -#define DECLARE_WAIT_QUEUE(name, tsk) \ +#define DECLARE_WAIT_QUEUE(name, tsk) \ wait_queue_t name = WAIT_QUEUE_INITIALIZER(name, tsk) - -void init_wait_queue(wait_queue_head_t * wqh); +void init_wait_queue(wait_queue_head_t *wqh); void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq); void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq); diff --git a/kernel/assert.c b/kernel/assert.c index f44057f..29f8b94 100644 --- a/kernel/assert.c +++ b/kernel/assert.c @@ -13,7 +13,8 @@ void assert_fail(char *exp, char *file, unsigned int line, char *func) { printk("%s:%d: %s: Assertion \'%s\' failed.\n", - file, line, func, exp); + file, line, func, exp); - while(1); + while (1) + ; } diff --git a/kernel/clock.c b/kernel/clock.c index 100762a..8526d1d 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -20,7 +20,7 @@ unsigned int sys_clock() return jiffies; } -void clk_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) +void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { jiffies++; diff --git a/kernel/cpuid.c b/kernel/cpuid.c index 6d94a79..e095f49 100644 --- a/kernel/cpuid.c +++ b/kernel/cpuid.c @@ -15,72 +15,75 @@ */ #include -#define TEST_FEATURE(val,bit,fea)\ -do{\ - if( ISSET_BIT(val,bit) )\ - printk(" %s",fea);\ -}while(0); +#define TEST_FEATURE(val, bit, fea) \ + do \ + { \ + if (ISSET_BIT(val, bit)) \ + printk(" %s", fea); \ + } while (0); -typedef struct reg{ unsigned long eax,ebx,ecx,edx; } reg_t; +typedef struct reg +{ + unsigned long eax, ebx, ecx, edx; +} reg_t; reg_t cpuid(unsigned long op) { reg_t r; asm("cpuid;" - :"=a"(r.eax), - "=b"(r.ebx), - "=c"(r.ecx), - "=d"(r.edx) - :"a"(op) - ); + : "=a"(r.eax), + "=b"(r.ebx), + "=c"(r.ecx), + "=d"(r.edx) + : "a"(op)); - return r; + return r; } -void detect_cpu() +void detect_cpu() { reg_t r; - unsigned short int cpu_sn[6]; //serial number - int i; + unsigned short int cpu_sn[6]; //serial number + int i; /**********************Get CPU Name********************************/ char cpu_name[13]; - - r=cpuid(0); + + r = cpuid(0); memcpy(cpu_name + 0, &r.ebx, 4); memcpy(cpu_name + 4, &r.edx, 4); memcpy(cpu_name + 8, &r.ecx, 4); cpu_name[12] = 0; - printk("%s ",cpu_name); + printk("%s ", cpu_name); - /**********************Get Processor Brand String******************/ - char pbs[50]; //processor brand string + /**********************Get Processor Brand String******************/ + char pbs[50]; //processor brand string r = cpuid(0x80000002); - memcpy(pbs + 0 , &r.eax, 4); - memcpy(pbs + 4 , &r.ebx, 4); - memcpy(pbs + 8 , &r.ecx, 4); + memcpy(pbs + 0, &r.eax, 4); + memcpy(pbs + 4, &r.ebx, 4); + memcpy(pbs + 8, &r.ecx, 4); memcpy(pbs + 12, &r.edx, 4); - r=cpuid(0x80000003); - memcpy(pbs + 16 , &r.eax, 4); - memcpy(pbs + 20 , &r.ebx, 4); - memcpy(pbs + 24 , &r.ecx, 4); - memcpy(pbs + 28 , &r.edx, 4); - r=cpuid(0x80000004); - memcpy(pbs + 32 , &r.eax, 4); - memcpy(pbs + 36 , &r.ebx, 4); - memcpy(pbs + 40 , &r.ecx, 4); - memcpy(pbs + 44 , &r.edx, 4); + r = cpuid(0x80000003); + memcpy(pbs + 16, &r.eax, 4); + memcpy(pbs + 20, &r.ebx, 4); + memcpy(pbs + 24, &r.ecx, 4); + memcpy(pbs + 28, &r.edx, 4); + r = cpuid(0x80000004); + memcpy(pbs + 32, &r.eax, 4); + memcpy(pbs + 36, &r.ebx, 4); + memcpy(pbs + 40, &r.ecx, 4); + memcpy(pbs + 44, &r.edx, 4); pbs[48] = 0; - printk("%s",pbs); + printk("%s", pbs); - /**********************Get Number of Processors********************/ - int pn;//number of logical processors in one physical processor - r=cpuid(1); - pn = ((r.ebx & 0x00FF0000) >> 16); - printk(" x %d Cores\n",pn); + /**********************Get Number of Processors********************/ + int pn; //number of logical processors in one physical processor + r = cpuid(1); + pn = ((r.ebx & 0x00FF0000) >> 16); + printk(" x %d Cores\n", pn); - /**********************Get the CPU's Feature***********************/ - int fv = r.edx; + /**********************Get the CPU's Feature***********************/ + int fv = r.edx; TEST_FEATURE(fv, 1, "fpu") TEST_FEATURE(fv, 2, "vme") TEST_FEATURE(fv, 3, "de") @@ -93,7 +96,7 @@ void detect_cpu() TEST_FEATURE(fv, 10, "Reserved") TEST_FEATURE(fv, 11, "SYSENTER/SYSEXIT") TEST_FEATURE(fv, 12, "mttr") - TEST_FEATURE(fv, 13, "pge") + TEST_FEATURE(fv, 13, "pge") TEST_FEATURE(fv, 14, "mca") TEST_FEATURE(fv, 15, "cmov") TEST_FEATURE(fv, 16, "pat") @@ -115,9 +118,10 @@ void detect_cpu() printk("\n"); - if(!((1UL<<11) & fv)) + if (!((1UL << 11) & fv)) { printk("Your CPU Do Not Support SYSENTER/SYSEXIT\n"); - while(1); + while (1) + ; } } diff --git a/kernel/exec.c b/kernel/exec.c index 8732ff9..1e1d437 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -32,107 +32,104 @@ void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) unsigned int npde = get_npd(vaddr); unsigned int npte = get_npt(vaddr); - pde_t *page_dir = (pde_t *) current->cr3; - pte_t *page_table = (pte_t *) PAGE_ALIGN(page_dir[npde]); + pde_t *page_dir = (pde_t *)current->cr3; + pte_t *page_table = (pte_t *)PAGE_ALIGN(page_dir[npde]); - if(page_table == 0) + if (page_table == 0) { - page_table = (pte_t *) alloc_one_page(0); + page_table = (pte_t *)alloc_one_page(0); memset(page_table, 0, PAGE_SIZE); - page_table = (pte_t *) va2pa(page_table); + page_table = (pte_t *)va2pa(page_table); assert(page_table != 0); } - page_dir[npde] = (unsigned long) page_table | flags | PAGE_P | PAGE_WR; + page_dir[npde] = (unsigned long)page_table | flags | PAGE_P | PAGE_WR; page_table = pa2va(page_table); page_table[npte] = paddr | flags; } int sysc_exec(const char *path, char *const argv[]) { - assert(argv == NULL); // unsupport now + assert(argv == NULL); // unsupport now unsigned int ino = namei(path); - if(ino == 0) + if (ino == 0) return -ENOENT; ext2_inode_t inode; ext2_read_inode(ino, &inode); - Elf32_Ehdr *ehdr= (Elf32_Ehdr *)alloc_one_page(0); + Elf32_Ehdr *ehdr = (Elf32_Ehdr *)alloc_one_page(0); assert(ehdr != 0); ext2_read_data(&inode, 0, PAGE_SIZE, (char *)ehdr); printk("%08x\n", *((unsigned long *)ehdr->e_ident)); - assert(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG)-1) == 0); + assert(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG) - 1) == 0); printk("Elf Entry: %08x\n", ehdr->e_entry); int i, j; - for(i=0; ie_phnum; ++i) + for (i = 0; i < ehdr->e_phnum; ++i) { Elf32_Phdr *phdr; - phdr = (Elf32_Phdr*)(((unsigned long)ehdr) + ehdr->e_phoff + (i*ehdr->e_phentsize)); + phdr = (Elf32_Phdr *)(((unsigned long)ehdr) + ehdr->e_phoff + (i * ehdr->e_phentsize)); printk("Type %08x Off %08x Va %08x Pa %08x Fsz %08x Mmsz %08x\n", - phdr->p_type, phdr->p_offset, phdr->p_vaddr, phdr->p_paddr, - phdr->p_filesz, phdr->p_memsz); - + phdr->p_type, phdr->p_offset, phdr->p_vaddr, phdr->p_paddr, + phdr->p_filesz, phdr->p_memsz); unsigned long vaddr = phdr->p_vaddr; - unsigned long offset= phdr->p_offset; - unsigned long mmsz = phdr->p_memsz; - unsigned long filesz= phdr->p_filesz; + unsigned long offset = phdr->p_offset; + unsigned long mmsz = phdr->p_memsz; + unsigned long filesz = phdr->p_filesz; - if(phdr->p_type != PT_LOAD) + if (phdr->p_type != PT_LOAD) continue; assert(mmsz >= filesz); unsigned int pgcnt = (mmsz + PAGE_SIZE - 1) / PAGE_SIZE; - unsigned int blkcnt= (filesz + EXT2_BLOCK_SIZE - 1) / EXT2_BLOCK_SIZE; + unsigned int blkcnt = (filesz + EXT2_BLOCK_SIZE - 1) / EXT2_BLOCK_SIZE; - void *buf = kmalloc(pgcnt*PAGE_SIZE, PAGE_SIZE); + void *buf = kmalloc(pgcnt * PAGE_SIZE, PAGE_SIZE); assert(PAGE_ALIGN(buf) == (unsigned long)buf); assert(buf != 0); + printk("vvvbufsz %08x datasz %08x\n", pgcnt * PAGE_SIZE, blkcnt * EXT2_BLOCK_SIZE); - printk("vvvbufsz %08x datasz %08x\n", pgcnt*PAGE_SIZE, blkcnt*EXT2_BLOCK_SIZE); - - if(filesz > 0) - ext2_read_data(&inode, offset, blkcnt*EXT2_BLOCK_SIZE, buf); + if (filesz > 0) + ext2_read_data(&inode, offset, blkcnt * EXT2_BLOCK_SIZE, buf); - for(j=0; jss = SELECTOR_USER_DS; - regs->ds = SELECTOR_USER_DS; - regs->es = SELECTOR_USER_DS; - regs->fs = SELECTOR_USER_DS; - regs->gs = SELECTOR_USER_DS; - regs->esp = (KRNLADDR-4*sizeof(unsigned long)); - regs->eflags= 0x200; - regs->cs = SELECTOR_USER_CS; + memset((void *)regs, 0, sizeof(pt_regs_t)); + regs->ss = SELECTOR_USER_DS; + regs->ds = SELECTOR_USER_DS; + regs->es = SELECTOR_USER_DS; + regs->fs = SELECTOR_USER_DS; + regs->gs = SELECTOR_USER_DS; + regs->esp = (KRNLADDR - 4 * sizeof(unsigned long)); + regs->eflags = 0x200; + regs->cs = SELECTOR_USER_CS; #endif - regs->eip = (unsigned long)ehdr->e_entry; - regs->edx = regs->eip; - regs->ecx = KRNLADDR; //(0xC0000000 - 16); + regs->eip = (unsigned long)ehdr->e_entry; + regs->edx = regs->eip; + regs->ecx = KRNLADDR; //(0xC0000000 - 16); //kfree(buf); free_pages((unsigned long)ehdr); - asm("movl $0, %%eax; movl %%ebx,%%ebp; movl %%ebp,%%esp;jmp syscall_exit;"::"b"((unsigned long)(regs))); + asm("movl $0, %%eax; movl %%ebx,%%ebp; movl %%ebp,%%esp;jmp syscall_exit;" ::"b"((unsigned long)(regs))); return 0; } diff --git a/kernel/fork.c b/kernel/fork.c index 8448f19..041c3a2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -24,95 +24,93 @@ extern pid_t get_next_pid(); int do_fork(pt_regs_t *regs, unsigned long flags) { - static int forkcnt = 7; task_union *tsk; tsk = alloc_task_union(); printk("fork task %08x flags %08x\n", tsk, flags); - if(tsk == NULL) + if (tsk == NULL) panic("can not malloc PCB"); memcpy(tsk, current, sizeof(task_union)); //{ - tsk->cr3 = (unsigned long) alloc_one_page(0); - assert(tsk->cr3 != 0); + tsk->cr3 = (unsigned long)alloc_one_page(0); + assert(tsk->cr3 != 0); - unsigned int i, j; - pde_t *pde_src = (pde_t*) current->cr3; - pde_t *pde_dst = (pde_t*) tsk->cr3; + unsigned int i, j; + pde_t *pde_src = (pde_t *)current->cr3; + pde_t *pde_dst = (pde_t *)tsk->cr3; - memcpy((void *)tsk->cr3, (void*)current->cr3, PAGE_SIZE); + memcpy((void *)tsk->cr3, (void *)current->cr3, PAGE_SIZE); - for(i=0; i= 768) { - unsigned long spde = (unsigned long) pde_src[i]; - unsigned long dpde = 0; + pde_dst[i] = pde_src[i]; + continue; + } - if(i>=768) - { - pde_dst[i] = pde_src[i]; - continue; - } + if (pde_src[i] == 0) + continue; - if(pde_src[i] == 0) - continue; + if (PAGE_ALIGN(spde) != 0) + { + dpde = alloc_one_page(0); + assert(dpde != 0); + memset((void *)dpde, 0, PAGE_SIZE); + dpde = PAGE_FLAGS(spde) | (unsigned long)va2pa(dpde); + } + else + { + pde_dst[i] = 0; + continue; + } + pde_dst[i] = dpde; - if(PAGE_ALIGN(spde) != 0) - { - dpde = alloc_one_page(0); - assert(dpde != 0); - memset((void*)dpde, 0, PAGE_SIZE); - dpde = PAGE_FLAGS(spde) | (unsigned long) va2pa(dpde); - } - else - { - pde_dst[i] = 0; - continue; - } - pde_dst[i] = dpde; - - pte_t *pte_src = pa2va(PAGE_ALIGN(spde)); - pte_t *pte_dst = pa2va(PAGE_ALIGN(dpde)); - for(j=0; j< PAGE_PTE_CNT; ++j) - { - pte_src[j] &= ~PAGE_WR; - pte_dst[j] = pte_src[j]; - - if(pte_src[j] == 0) - continue; - printk("----pde[%u] pte_src[%u] %08x\n", i, j, pte_src[j]); - page_t *page = pa2page(pte_src[j]); - page->count ++; - } + pte_t *pte_src = pa2va(PAGE_ALIGN(spde)); + pte_t *pte_dst = pa2va(PAGE_ALIGN(dpde)); + for (j = 0; j < PAGE_PTE_CNT; ++j) + { + pte_src[j] &= ~PAGE_WR; + pte_dst[j] = pte_src[j]; + if (pte_src[j] == 0) + continue; + printk("----pde[%u] pte_src[%u] %08x\n", i, j, pte_src[j]); + page_t *page = pa2page(pte_src[j]); + page->count++; } + } //} load_cr3(current); - tsk->pid = get_next_pid(); - tsk->ppid = current->pid; + tsk->pid = get_next_pid(); + tsk->ppid = current->pid; - pt_regs_t *child_regs = ((pt_regs_t *) (TASK_SIZE+(unsigned long) tsk)) - 1; + pt_regs_t *child_regs = ((pt_regs_t *)(TASK_SIZE + (unsigned long)tsk)) - 1; *child_regs = *regs; child_regs->eax = 0; child_regs->eflags |= 0x200; //enable IF - tsk->esp0 = TASK_SIZE + (unsigned long) tsk; - tsk->esp = (unsigned long) child_regs; - tsk->eip = (unsigned long) ret_from_fork_user; + tsk->esp0 = TASK_SIZE + (unsigned long)tsk; + tsk->esp = (unsigned long)child_regs; + tsk->eip = (unsigned long)ret_from_fork_user; - if(flags & FORK_KRNL) + if (flags & FORK_KRNL) { - tsk->eip= (unsigned long) ret_from_fork_krnl; + tsk->eip = (unsigned long)ret_from_fork_krnl; } printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0); tsk->state = TASK_RUNNING; - tsk->weight= TASK_INIT_WEIGHT; + tsk->weight = TASK_INIT_WEIGHT; INIT_LIST_HEAD(&tsk->list); diff --git a/kernel/i8259.c b/kernel/i8259.c index 29a317f..6a7e5cf 100644 --- a/kernel/i8259.c +++ b/kernel/i8259.c @@ -14,7 +14,6 @@ *-------------------------------------------------------------------------- */ - #include "io.h" #include "i8259.h" #include "irq.h" @@ -29,15 +28,15 @@ void mask_i8259() void init_i8259() { //Master... - outb_p(0x11, PIC_MASTER_CMD); // ICW1 - outb_p(0x20, PIC_MASTER_IMR); // ICW2: IR0-7 mapped to 0x20-0x27 - outb_p((1U< #include +void root_task_entry(); -void root_task_entry(); +TSS tss; +System system; +Desc idt[NIDT]; +Desc gdt[NGDT]; -TSS tss; -System system; -Desc idt[NIDT]; -Desc gdt[NGDT]; - -char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); +char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__((__aligned__(PAGE_SIZE))); int debug_wait_queue_get(); @@ -40,7 +39,7 @@ void init_task_entry() int cnt = 0; pid_t id = sysc_getpid(); - while(1) + while (1) { sysc_test(); //printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); @@ -52,8 +51,8 @@ void init_task_entry() void kernel_task(void *entry) { pt_regs_t regs; - memset((void*)®s, 0, sizeof(regs)); - regs.edx = (unsigned long) entry; + memset((void *)®s, 0, sizeof(regs)); + regs.edx = (unsigned long)entry; int pid = do_fork(®s, FORK_KRNL); printk("kernel task pid is %d\n", pid); enable_irq(); @@ -66,9 +65,8 @@ void root_task_entry() kernel_task(user_task_entry); kernel_task(init_task_entry); - int cnt = 0; - while(1) + while (1) { sysc_test(); //printl(MPL_ROOT, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++); diff --git a/kernel/innerint.c b/kernel/innerint.c index 7e064e8..3c135da 100644 --- a/kernel/innerint.c +++ b/kernel/innerint.c @@ -19,12 +19,15 @@ #include #include -#define DIE_MSG() do{\ - printk("Unsupport Now...[%s]\n", __FUNCTION__); \ - printk("EFLAGS:%08x CS:%02x EIP:%08x ERRCODE:%x", \ - regs.eflags, regs.cs, regs.eip, regs.errcode); \ - while(1); \ -}while(0); +#define DIE_MSG() \ + do \ + { \ + printk("Unsupport Now...[%s]\n", __FUNCTION__); \ + printk("EFLAGS:%08x CS:%02x EIP:%08x ERRCODE:%x", \ + regs.eflags, regs.cs, regs.eip, regs.errcode); \ + while (1) \ + ; \ + } while (0); void doDivideError(pt_regs_t regs) { @@ -99,16 +102,17 @@ US RW P - Description 1 1 1 - User process tried to write a page and caused a protection fault #endif //DIE_MSG(); - void *addr; - u32 errcode = regs.errcode; + void *addr; + u32 errcode = regs.errcode; - asm("movl %%cr2,%%eax":"=a"(addr)); + asm("movl %%cr2,%%eax" + : "=a"(addr)); //printk("do page fault errcode %x addr %08x [%08x]\n", errcode, addr, current); //assert(errcode != 2 && errcode != 6); - if((errcode & PAGE_P) == 0) + if ((errcode & PAGE_P) == 0) { do_no_page(addr); } diff --git a/kernel/irq.c b/kernel/irq.c index ed48e87..57f2e2f 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -22,32 +22,30 @@ irq_desc_t irq_desc[NR_IRQS]; -int enable_no_irq_chip(unsigned int irq){return 0;} -int disable_no_irq_chip(unsigned int irq){return 0;} +int enable_no_irq_chip(unsigned int irq) { return 0; } +int disable_no_irq_chip(unsigned int irq) { return 0; } irq_chip_t no_irq_chip = -{ - .name = "none", - .enable = enable_no_irq_chip, - .disable= disable_no_irq_chip -}; + { + .name = "none", + .enable = enable_no_irq_chip, + .disable = disable_no_irq_chip}; irq_desc_t no_irq_desc = -{ - .chip = &no_irq_chip, - .action = NULL, - .status = 0, - .depth = 0 -}; + { + .chip = &no_irq_chip, + .action = NULL, + .status = 0, + .depth = 0}; -__attribute__ ((regparm(1))) void irq_handler(pt_regs_t *regs) +__attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) { unsigned int irq = regs->irq; - if(irq >= NR_IRQS) + if (irq >= NR_IRQS) { printk("invalid irq %d\n", irq); - return ; + return; } irq_desc_t *p = irq_desc + irq; @@ -57,12 +55,13 @@ __attribute__ ((regparm(1))) void irq_handler(pt_regs_t *regs) atomic_inc(&(current->preempt_cnt)); unsigned long esp; - asm("movl %%esp, %%eax":"=a"(esp)); + asm("movl %%esp, %%eax" + : "=a"(esp)); printl(MPL_PREEMPT, "current %08x cr3 %08x preempt %d esp %08x", current, current->cr3, current->preempt_cnt, esp); p->chip->ack(irq); sti(); - while(action && action->handler) + while (action && action->handler) { action->handler(irq, regs, action->dev_id); action = action->next; @@ -73,7 +72,6 @@ __attribute__ ((regparm(1))) void irq_handler(pt_regs_t *regs) atomic_dec(&(current->preempt_cnt)); } - int request_irq(unsigned int irq, void (*handler)(unsigned int, pt_regs_t *, void *), const char *devname, @@ -81,32 +79,31 @@ int request_irq(unsigned int irq, { irq_action_t *p; - if(irq >= NR_IRQS) + if (irq >= NR_IRQS) return -EINVAL; - if(handler == NULL) + if (handler == NULL) return -EINVAL; // 检查是否已经注册过处理函数了 p = irq_desc[irq].action; - while(p != NULL) + while (p != NULL) { - if(p->handler == handler) + if (p->handler == handler) return 0; p = p->next; } - p = (irq_action_t *) kmalloc(sizeof(irq_action_t), 0); - if(p == NULL) + p = (irq_action_t *)kmalloc(sizeof(irq_action_t), 0); + if (p == NULL) return -ENOMEM; - - p->dev_name = devname; - p->dev_id = dev_id; - p->handler = handler; - p->next = NULL; - if(irq_desc[irq].action != NULL) + p->dev_name = devname; + p->dev_id = dev_id; + p->handler = handler; + p->next = NULL; + if (irq_desc[irq].action != NULL) { - p->next = irq_desc[irq].action; + p->next = irq_desc[irq].action; //printk("p->next:%x\n", p->next); } irq_desc[irq].action = p; @@ -114,8 +111,6 @@ int request_irq(unsigned int irq, return 0; } - - int open_irq(unsigned int irq) { return irq_desc[irq].chip->enable(irq); diff --git a/kernel/pci.c b/kernel/pci.c index 9e5a47a..365a0b9 100644 --- a/kernel/pci.c +++ b/kernel/pci.c @@ -66,26 +66,26 @@ void scan_pci_bus(int bus) u32 v; int i; printk("scanning pci bus %d\n", bus); - - for(dev=0; dev<32; dev++) + + for (dev = 0; dev < 32; dev++) { - for(devfn =0; devfn<8; devfn++) + for (devfn = 0; devfn < 8; devfn++) { cmd = PCI_CMD(bus, dev, devfn, PCI_VENDORID); v = pci_read_config_word(cmd); - if(v == 0xFFFF) + if (v == 0xFFFF) continue; pci_device_t *pci = kmalloc(sizeof(pci_device_t), 0); - if(0 == pci) + if (0 == pci) { printk("no space to alloc for pci_device_t\n"); continue; } - pci->bus = bus; - pci->dev = dev; - pci->devfn = devfn; + pci->bus = bus; + pci->dev = dev; + pci->devfn = devfn; pci->vendor = v; @@ -100,21 +100,20 @@ void scan_pci_bus(int bus) cmd = PCI_CMD(bus, dev, devfn, PCI_CLASSCODE); pci->classcode = pci_read_config_word(cmd); - + cmd = PCI_CMD(bus, dev, devfn, PCI_INTRLINE); pci->intr_line = pci_read_config_byte(cmd); cmd = PCI_CMD(bus, dev, devfn, PCI_INTRPIN); pci->intr_pin = pci_read_config_byte(cmd); - cmd = PCI_CMD(bus, dev, devfn, PCI_HDRTYPE); pci->hdr_type = pci_read_config_byte(cmd); pci->hdr_type &= PCI_HDRTYPE_MASK; - for(i=0; ibars[i] = pci_read_config_long(cmd); } @@ -123,7 +122,6 @@ void scan_pci_bus(int bus) } } - pci_device_t *pci_find_device(unsigned int vendor, unsigned int device) { int i; @@ -134,11 +132,10 @@ pci_device_t *pci_find_device(unsigned int vendor, unsigned int device) { pci = list_entry(p, pci_device_t, list); - if(pci->vendor == vendor && pci->device == device) + if (pci->vendor == vendor && pci->device == device) return pci; } - return 0; } @@ -152,7 +149,7 @@ pci_device_t *pci_find_device_by_classcode(unsigned int classcode) { pci = list_entry(p, pci_device_t, list); - if(pci->classcode == classcode) + if (pci->classcode == classcode) return pci; } @@ -170,7 +167,7 @@ void dump_pci_dev() printk("vendor %04x device %04x class %04x intr %3d ", pci->vendor, pci->device, pci->classcode, pci->intr_line); printk("%s\n", pci_get_info(pci->classcode, pci->progif)); continue; - switch(pci->hdr_type) + switch (pci->hdr_type) { case PCI_HDRTYPE_NORMAL: printk("Normal Device\n"); @@ -192,31 +189,31 @@ int probe_pci_bus() { int v; int cmd; - int dev,devfn; + int dev, devfn; // Check if The IO Address was Used... - cmd = PCI_CMD(0,0,0,0); + cmd = PCI_CMD(0, 0, 0, 0); v = pci_read_config_long(cmd); - if( v == 0xFFFFFFFF || v == 0x00000000 || v == 0x0000FFFF || v == 0xFFFF0000) + if (v == 0xFFFFFFFF || v == 0x00000000 || v == 0x0000FFFF || v == 0xFFFF0000) goto err; // Maybe it's just an ISA Device // So We Must Check if It is PCI... - for(dev=0; dev<32; dev++) + for (dev = 0; dev < 32; dev++) { - for(devfn=0; devfn<8; devfn++) + for (devfn = 0; devfn < 8; devfn++) { cmd = PCI_CMD(0, dev, devfn, PCI_CLASSCODE); v = pci_read_config_word(cmd); - if(v == PCI_CLASS_BRIDGE_HOST || v == PCI_CLASS_DISPLAY_VGA) + if (v == PCI_CLASS_BRIDGE_HOST || v == PCI_CLASS_DISPLAY_VGA) return 1; cmd = PCI_CMD(0, dev, devfn, PCI_VENDORID); v = pci_read_config_word(cmd); - if(v == PCI_VENDORID_INTEL || v == PCI_VENDORID_COMPAQ) + if (v == PCI_VENDORID_INTEL || v == PCI_VENDORID_COMPAQ) return 1; } } @@ -228,183 +225,183 @@ err: return 0; } -void setup_pci() +void setup_pci() { - if(!probe_pci_bus()) - return ; + if (!probe_pci_bus()) + return; scan_pci_bus(0); dump_pci_dev(); } -typedef struct pci_info { +typedef struct pci_info +{ unsigned long code; - unsigned int flag; - const char *info; - const char *detail; + unsigned int flag; + const char *info; + const char *detail; } pci_info_t; pci_info_t pci_info[] = { - { 0x000000, 0, "VGA-Compatible devices", "Any device except for VGA-Compatible devices" }, - { 0x000100, 0, "VGA-Compatible device", "VGA-Compatible Device" }, - { 0x010000, 0, "SCSI Bus Controller", "SCSI Bus Controller" }, - { 0x0101, 1, "IDE Controller", "IDE Controller" }, - { 0x010200, 0, "Floppy Disk Controller", "Floppy Disk Controller" }, - { 0x010300, 0, "IPI Bus Controller", "IPI Bus Controller" }, - { 0x010400, 0, "RAID Controller", "RAID Controller" }, - { 0x010520, 0, "ATA Controller", "ATA Controller (Single DMA)" }, - { 0x010530, 0, "ATA Controller", "ATA Controller (Chained DMA)" }, - { 0x010600, 0, "Serial ATA", "Serial ATA (Vendor Specific Interface)" }, - { 0x010601, 0, "Serial ATA", "Serial ATA (AHCI 1.0)" }, - { 0x010700, 0, "SCSI", "Serial Attached SCSI (SAS)" }, - { 0x018000, 0, "Storage Controller", "Other Mass Storage Controller" }, - { 0x020000, 0, "Ethernet Controller", "Ethernet Controller" }, - { 0x020100, 0, "Token Ring Controller", "Token Ring Controller" }, - { 0x020200, 0, "FDDI Controller", "FDDI Controller" }, - { 0x020300, 0, "ATM Controller", "ATM Controller" }, - { 0x020400, 0, "ISDN Controller", "ISDN Controller" }, - { 0x020500, 0, "WorldFip Controller", "WorldFip Controller" }, - { 0x0206, 1, "PICMG 2.14", "PICMG 2.14 Multi Computing" }, - { 0x028000, 0, "Network Controller", "Other Network Controller" }, - { 0x030000, 0, "VGA-Compatible Controller", "VGA-Compatible Controller" }, - { 0x030001, 0, "8512-Compatible Controller", "8512-Compatible Controller" }, - { 0x030100, 0, "XGA Controller", "XGA Controller" }, - { 0x030200, 0, "3D Controller (Not VGA-Compatible)", "3D Controller (Not VGA-Compatible)" }, - { 0x038000, 0, "Display Controller", "Other Display Controller" }, - { 0x040000, 0, "Video Device", "Video Device" }, - { 0x040100, 0, "Audio Device", "Audio Device" }, - { 0x040200, 0, "Computer Telephony Device", "Computer Telephony Device" }, - { 0x048000, 0, "Other Multimedia Device", "Other Multimedia Device" }, - { 0x050000, 0, "RAM Controller", "RAM Controller" }, - { 0x050100, 0, "Flash Controller", "Flash Controller" }, - { 0x058000, 0, "Memory Controller", "Other Memory Controller" }, - { 0x060000, 0, "Host Bridge", "Host Bridge" }, - { 0x060100, 0, "ISA Bridge", "ISA Bridge" }, - { 0x060200, 0, "EISA Bridge", "EISA Bridge" }, - { 0x060300, 0, "MCA Bridge", "MCA Bridge" }, - { 0x060400, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge" }, - { 0x060401, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Subtractive Decode)" }, - { 0x060500, 0, "PCMCIA Bridge", "PCMCIA Bridge" }, - { 0x060600, 0, "NuBus Bridge", "NuBus Bridge" }, - { 0x060700, 0, "CardBus Bridge", "CardBus Bridge" }, - { 0x0608, 1, "RACEway Bridge", "RACEway Bridge" }, - { 0x060940, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Semi-Transparent, Primary)" }, - { 0x060980, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Semi-Transparent, Secondary)" }, - { 0x060A00, 0, "InfiniBrand-to-PCI Host Bridge", "InfiniBrand-to-PCI Host Bridge" }, - { 0x068000, 0, "Bridge Device", "Other Bridge Device" }, - { 0x070000, 0, "Serial Controller", "Generic XT-Compatible Serial Controller" }, - { 0x070001, 0, "Serial Controller", "16450-Compatible Serial Controller" }, - { 0x070002, 0, "Serial Controller", "16550-Compatible Serial Controller" }, - { 0x070003, 0, "Serial Controller", "16650-Compatible Serial Controller" }, - { 0x070004, 0, "Serial Controller", "16750-Compatible Serial Controller" }, - { 0x070005, 0, "Serial Controller", "16850-Compatible Serial Controller" }, - { 0x070006, 0, "Serial Controller", "16950-Compatible Serial Controller" }, - { 0x070100, 0, "Parallel Port", "Parallel Port" }, - { 0x070101, 0, "Parallel Port", "Bi-Directional Parallel Port" }, - { 0x070102, 0, "X Parallel Port", "ECP 1.X Compliant Parallel Port" }, - { 0x070103, 0, "IEEE 1284 Controller", "IEEE 1284 Controller" }, - { 0x0701FE, 0, "IEEE 1284 Target Device", "IEEE 1284 Target Device" }, - { 0x070200, 0, "Serial Controller", "Multiport Serial Controller" }, - { 0x070300, 0, "Generic Modem", "Generic Modem" }, - { 0x070301, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16450-Compatible Interface)" }, - { 0x070302, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16550-Compatible Interface)" }, - { 0x070303, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16650-Compatible Interface)" }, - { 0x070304, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16750-Compatible Interface)" }, - { 0x070400, 0, "IEEE 488.1/2 Controller", "IEEE 488.1/2 (GPIB) Controller" }, - { 0x070500, 0, "Smart Card", "Smart Card" }, - { 0x078000, 0, "Communications Device", "Other Communications Device" }, - { 0x080000, 0, "Generic 8259 PIC", "Generic 8259 PIC" }, - { 0x080001, 0, "ISA PIC", "ISA PIC" }, - { 0x080002, 0, "EISA PIC", "EISA PIC" }, - { 0x080010, 0, "APIC Interrupt Controller", "I/O APIC Interrupt Controller" }, - { 0x080020, 0, "APIC Interrupt Controller", "I/O(x) APIC Interrupt Controller" }, - { 0x080100, 0, "8237 DMA Controller", "Generic 8237 DMA Controller" }, - { 0x080101, 0, "ISA DMA Controller", "ISA DMA Controller" }, - { 0x080102, 0, "EISA DMA Controller", "EISA DMA Controller" }, - { 0x080200, 0, "8254 System Timer", "Generic 8254 System Timer" }, - { 0x080201, 0, "ISA System Timer", "ISA System Timer" }, - { 0x080202, 0, "EISA System Timer", "EISA System Timer" }, - { 0x080300, 0, "Generic RTC Controller", "Generic RTC Controller" }, - { 0x080301, 0, "ISA RTC Controller", "ISA RTC Controller" }, - { 0x080400, 0, "Generic PCI Hot-Plug Controller", "Generic PCI Hot-Plug Controller" }, - { 0x088000, 0, "Other System Peripheral", "Other System Peripheral" }, - { 0x090000, 0, "Keyboard Controller", "Keyboard Controller" }, - { 0x090100, 0, "Digitizer", "Digitizer" }, - { 0x090200, 0, "Mouse Controller", "Mouse Controller" }, - { 0x090300, 0, "Scanner Controller", "Scanner Controller" }, - { 0x090400, 0, "Gameport Controller (Generic)", "Gameport Controller (Generic)" }, - { 0x090410, 0, "Gameport Contrlller (Legacy)", "Gameport Contrlller (Legacy)" }, - { 0x098000, 0, "Other Input Controller", "Other Input Controller" }, - { 0x0A0000, 0, "Generic Docking Station", "Generic Docking Station" }, - { 0x0A8000, 0, "Other Docking Station", "Other Docking Station" }, - { 0x0B0000, 0, "386 Processor", "386 Processor" }, - { 0x0B0100, 0, "486 Processor", "486 Processor" }, - { 0x0B0200, 0, "Pentium Processor", "Pentium Processor" }, - { 0x0B1000, 0, "Alpha Processor", "Alpha Processor" }, - { 0x0B2000, 0, "PowerPC Processor", "PowerPC Processor" }, - { 0x0B3000, 0, "MIPS Processor", "MIPS Processor" }, - { 0x0B4000, 0, "Co-Processor", "Co-Processor" }, - { 0x0C0000, 0, "IEEE 1394 Controller", "IEEE 1394 Controller (FireWire)" }, - { 0x0C0010, 0, "IEEE 1394 Controller", "IEEE 1394 Controller (1394 OpenHCI Spec)" }, - { 0x0C0100, 0, "ACCESS.bus", "ACCESS.bus" }, - { 0x0C0200, 0, "SSA", "SSA" }, - { 0x0C0300, 0, "USB", "USB (Universal Host Controller Spec)" }, - { 0x0C0310, 0, "USB", "USB (Open Host Controller Spec" }, - { 0x0C0320, 0, "USB2 Host Controller", "USB2 Host Controller (Intel Enhanced Host Controller Interface)" }, - { 0x0C0380, 0, "USB", "USB" }, - { 0x0C03FE, 0, "USB", "USB (Not Host Controller)" }, - { 0x0C0400, 0, "Fibre Channel", "Fibre Channel" }, - { 0x0C0500, 0, "SMBus", "SMBus" }, - { 0x0C0600, 0, "InfiniBand", "InfiniBand" }, - { 0x0C0700, 0, "IPMI SMIC Interface", "IPMI SMIC Interface" }, - { 0x0C0701, 0, "IPMI Kybd Interface", "IPMI Kybd Controller Style Interface" }, - { 0x0C0702, 0, "IPMI Block Interface", "IPMI Block Transfer Interface" }, - { 0x0C0800, 0, "SERCOS Interface", "SERCOS Interface Standard (IEC 61491)" }, - { 0x0C0900, 0, "CANbus", "CANbus" }, - { 0x0D0000, 0, "iRDA Controller", "iRDA Compatible Controller" }, - { 0x0D0100, 0, "IR Controller", "Consumer IR Controller" }, - { 0x0D1000, 0, "RF Controller", "RF Controller" }, - { 0x0D1100, 0, "Bluetooth Controller", "Bluetooth Controller" }, - { 0x0D1200, 0, "Broadband Controller", "Broadband Controller" }, - { 0x0D2000, 0, "Ethernet Controller (802.11a)", "Ethernet Controller (802.11a)" }, - { 0x0D2100, 0, "Ethernet Controller (802.11b)", "Ethernet Controller (802.11b)" }, - { 0x0D8000, 0, "Wireless Controller", "Other Wireless Controller" }, - { 0x0E00, 1, "I20 Architecture", "I20 Architecture" }, - { 0x0E0000, 0, "Message FIFO", "Message FIFO" }, - { 0x0F0100, 0, "TV Controller", "TV Controller" }, - { 0x0F0200, 0, "Audio Controller", "Audio Controller" }, - { 0x0F0300, 0, "Voice Controller", "Voice Controller" }, - { 0x0F0400, 0, "Data Controller", "Data Controller" }, - { 0x100000, 0, "Computing Encrpytion/Decryption", "Network and Computing Encrpytion/Decryption" }, - { 0x101000, 0, "Entertainment Encryption/Decryption", "Entertainment Encryption/Decryption" }, - { 0x108000, 0, "Other Encryption/Decryption", "Other Encryption/Decryption" }, - { 0x110000, 0, "DPIO Modules", "DPIO Modules" }, - { 0x110100, 0, "Performance Counters", "Performance Counters" }, - { 0x111000, 0, "Communications Syncrhonization Plus Time and Frequency Test/Measurment", "Communications Syncrhonization Plus Time and Frequency Test/Measurment" }, - { 0x112000, 0, "Management Card", "Management Card" }, - { 0x118000, 0, "Acquisition/Signal Processing Controller", "Other Data Acquisition/Signal Processing Controller" }, - { 0x000000, 0, 0 } -}; + {0x000000, 0, "VGA-Compatible devices", "Any device except for VGA-Compatible devices"}, + {0x000100, 0, "VGA-Compatible device", "VGA-Compatible Device"}, + {0x010000, 0, "SCSI Bus Controller", "SCSI Bus Controller"}, + {0x0101, 1, "IDE Controller", "IDE Controller"}, + {0x010200, 0, "Floppy Disk Controller", "Floppy Disk Controller"}, + {0x010300, 0, "IPI Bus Controller", "IPI Bus Controller"}, + {0x010400, 0, "RAID Controller", "RAID Controller"}, + {0x010520, 0, "ATA Controller", "ATA Controller (Single DMA)"}, + {0x010530, 0, "ATA Controller", "ATA Controller (Chained DMA)"}, + {0x010600, 0, "Serial ATA", "Serial ATA (Vendor Specific Interface)"}, + {0x010601, 0, "Serial ATA", "Serial ATA (AHCI 1.0)"}, + {0x010700, 0, "SCSI", "Serial Attached SCSI (SAS)"}, + {0x018000, 0, "Storage Controller", "Other Mass Storage Controller"}, + {0x020000, 0, "Ethernet Controller", "Ethernet Controller"}, + {0x020100, 0, "Token Ring Controller", "Token Ring Controller"}, + {0x020200, 0, "FDDI Controller", "FDDI Controller"}, + {0x020300, 0, "ATM Controller", "ATM Controller"}, + {0x020400, 0, "ISDN Controller", "ISDN Controller"}, + {0x020500, 0, "WorldFip Controller", "WorldFip Controller"}, + {0x0206, 1, "PICMG 2.14", "PICMG 2.14 Multi Computing"}, + {0x028000, 0, "Network Controller", "Other Network Controller"}, + {0x030000, 0, "VGA-Compatible Controller", "VGA-Compatible Controller"}, + {0x030001, 0, "8512-Compatible Controller", "8512-Compatible Controller"}, + {0x030100, 0, "XGA Controller", "XGA Controller"}, + {0x030200, 0, "3D Controller (Not VGA-Compatible)", "3D Controller (Not VGA-Compatible)"}, + {0x038000, 0, "Display Controller", "Other Display Controller"}, + {0x040000, 0, "Video Device", "Video Device"}, + {0x040100, 0, "Audio Device", "Audio Device"}, + {0x040200, 0, "Computer Telephony Device", "Computer Telephony Device"}, + {0x048000, 0, "Other Multimedia Device", "Other Multimedia Device"}, + {0x050000, 0, "RAM Controller", "RAM Controller"}, + {0x050100, 0, "Flash Controller", "Flash Controller"}, + {0x058000, 0, "Memory Controller", "Other Memory Controller"}, + {0x060000, 0, "Host Bridge", "Host Bridge"}, + {0x060100, 0, "ISA Bridge", "ISA Bridge"}, + {0x060200, 0, "EISA Bridge", "EISA Bridge"}, + {0x060300, 0, "MCA Bridge", "MCA Bridge"}, + {0x060400, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge"}, + {0x060401, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Subtractive Decode)"}, + {0x060500, 0, "PCMCIA Bridge", "PCMCIA Bridge"}, + {0x060600, 0, "NuBus Bridge", "NuBus Bridge"}, + {0x060700, 0, "CardBus Bridge", "CardBus Bridge"}, + {0x0608, 1, "RACEway Bridge", "RACEway Bridge"}, + {0x060940, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Semi-Transparent, Primary)"}, + {0x060980, 0, "PCI-to-PCI Bridge", "PCI-to-PCI Bridge (Semi-Transparent, Secondary)"}, + {0x060A00, 0, "InfiniBrand-to-PCI Host Bridge", "InfiniBrand-to-PCI Host Bridge"}, + {0x068000, 0, "Bridge Device", "Other Bridge Device"}, + {0x070000, 0, "Serial Controller", "Generic XT-Compatible Serial Controller"}, + {0x070001, 0, "Serial Controller", "16450-Compatible Serial Controller"}, + {0x070002, 0, "Serial Controller", "16550-Compatible Serial Controller"}, + {0x070003, 0, "Serial Controller", "16650-Compatible Serial Controller"}, + {0x070004, 0, "Serial Controller", "16750-Compatible Serial Controller"}, + {0x070005, 0, "Serial Controller", "16850-Compatible Serial Controller"}, + {0x070006, 0, "Serial Controller", "16950-Compatible Serial Controller"}, + {0x070100, 0, "Parallel Port", "Parallel Port"}, + {0x070101, 0, "Parallel Port", "Bi-Directional Parallel Port"}, + {0x070102, 0, "X Parallel Port", "ECP 1.X Compliant Parallel Port"}, + {0x070103, 0, "IEEE 1284 Controller", "IEEE 1284 Controller"}, + {0x0701FE, 0, "IEEE 1284 Target Device", "IEEE 1284 Target Device"}, + {0x070200, 0, "Serial Controller", "Multiport Serial Controller"}, + {0x070300, 0, "Generic Modem", "Generic Modem"}, + {0x070301, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16450-Compatible Interface)"}, + {0x070302, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16550-Compatible Interface)"}, + {0x070303, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16650-Compatible Interface)"}, + {0x070304, 0, "Hayes Compatible Modem", "Hayes Compatible Modem (16750-Compatible Interface)"}, + {0x070400, 0, "IEEE 488.1/2 Controller", "IEEE 488.1/2 (GPIB) Controller"}, + {0x070500, 0, "Smart Card", "Smart Card"}, + {0x078000, 0, "Communications Device", "Other Communications Device"}, + {0x080000, 0, "Generic 8259 PIC", "Generic 8259 PIC"}, + {0x080001, 0, "ISA PIC", "ISA PIC"}, + {0x080002, 0, "EISA PIC", "EISA PIC"}, + {0x080010, 0, "APIC Interrupt Controller", "I/O APIC Interrupt Controller"}, + {0x080020, 0, "APIC Interrupt Controller", "I/O(x) APIC Interrupt Controller"}, + {0x080100, 0, "8237 DMA Controller", "Generic 8237 DMA Controller"}, + {0x080101, 0, "ISA DMA Controller", "ISA DMA Controller"}, + {0x080102, 0, "EISA DMA Controller", "EISA DMA Controller"}, + {0x080200, 0, "8254 System Timer", "Generic 8254 System Timer"}, + {0x080201, 0, "ISA System Timer", "ISA System Timer"}, + {0x080202, 0, "EISA System Timer", "EISA System Timer"}, + {0x080300, 0, "Generic RTC Controller", "Generic RTC Controller"}, + {0x080301, 0, "ISA RTC Controller", "ISA RTC Controller"}, + {0x080400, 0, "Generic PCI Hot-Plug Controller", "Generic PCI Hot-Plug Controller"}, + {0x088000, 0, "Other System Peripheral", "Other System Peripheral"}, + {0x090000, 0, "Keyboard Controller", "Keyboard Controller"}, + {0x090100, 0, "Digitizer", "Digitizer"}, + {0x090200, 0, "Mouse Controller", "Mouse Controller"}, + {0x090300, 0, "Scanner Controller", "Scanner Controller"}, + {0x090400, 0, "Gameport Controller (Generic)", "Gameport Controller (Generic)"}, + {0x090410, 0, "Gameport Contrlller (Legacy)", "Gameport Contrlller (Legacy)"}, + {0x098000, 0, "Other Input Controller", "Other Input Controller"}, + {0x0A0000, 0, "Generic Docking Station", "Generic Docking Station"}, + {0x0A8000, 0, "Other Docking Station", "Other Docking Station"}, + {0x0B0000, 0, "386 Processor", "386 Processor"}, + {0x0B0100, 0, "486 Processor", "486 Processor"}, + {0x0B0200, 0, "Pentium Processor", "Pentium Processor"}, + {0x0B1000, 0, "Alpha Processor", "Alpha Processor"}, + {0x0B2000, 0, "PowerPC Processor", "PowerPC Processor"}, + {0x0B3000, 0, "MIPS Processor", "MIPS Processor"}, + {0x0B4000, 0, "Co-Processor", "Co-Processor"}, + {0x0C0000, 0, "IEEE 1394 Controller", "IEEE 1394 Controller (FireWire)"}, + {0x0C0010, 0, "IEEE 1394 Controller", "IEEE 1394 Controller (1394 OpenHCI Spec)"}, + {0x0C0100, 0, "ACCESS.bus", "ACCESS.bus"}, + {0x0C0200, 0, "SSA", "SSA"}, + {0x0C0300, 0, "USB", "USB (Universal Host Controller Spec)"}, + {0x0C0310, 0, "USB", "USB (Open Host Controller Spec"}, + {0x0C0320, 0, "USB2 Host Controller", "USB2 Host Controller (Intel Enhanced Host Controller Interface)"}, + {0x0C0380, 0, "USB", "USB"}, + {0x0C03FE, 0, "USB", "USB (Not Host Controller)"}, + {0x0C0400, 0, "Fibre Channel", "Fibre Channel"}, + {0x0C0500, 0, "SMBus", "SMBus"}, + {0x0C0600, 0, "InfiniBand", "InfiniBand"}, + {0x0C0700, 0, "IPMI SMIC Interface", "IPMI SMIC Interface"}, + {0x0C0701, 0, "IPMI Kybd Interface", "IPMI Kybd Controller Style Interface"}, + {0x0C0702, 0, "IPMI Block Interface", "IPMI Block Transfer Interface"}, + {0x0C0800, 0, "SERCOS Interface", "SERCOS Interface Standard (IEC 61491)"}, + {0x0C0900, 0, "CANbus", "CANbus"}, + {0x0D0000, 0, "iRDA Controller", "iRDA Compatible Controller"}, + {0x0D0100, 0, "IR Controller", "Consumer IR Controller"}, + {0x0D1000, 0, "RF Controller", "RF Controller"}, + {0x0D1100, 0, "Bluetooth Controller", "Bluetooth Controller"}, + {0x0D1200, 0, "Broadband Controller", "Broadband Controller"}, + {0x0D2000, 0, "Ethernet Controller (802.11a)", "Ethernet Controller (802.11a)"}, + {0x0D2100, 0, "Ethernet Controller (802.11b)", "Ethernet Controller (802.11b)"}, + {0x0D8000, 0, "Wireless Controller", "Other Wireless Controller"}, + {0x0E00, 1, "I20 Architecture", "I20 Architecture"}, + {0x0E0000, 0, "Message FIFO", "Message FIFO"}, + {0x0F0100, 0, "TV Controller", "TV Controller"}, + {0x0F0200, 0, "Audio Controller", "Audio Controller"}, + {0x0F0300, 0, "Voice Controller", "Voice Controller"}, + {0x0F0400, 0, "Data Controller", "Data Controller"}, + {0x100000, 0, "Computing Encrpytion/Decryption", "Network and Computing Encrpytion/Decryption"}, + {0x101000, 0, "Entertainment Encryption/Decryption", "Entertainment Encryption/Decryption"}, + {0x108000, 0, "Other Encryption/Decryption", "Other Encryption/Decryption"}, + {0x110000, 0, "DPIO Modules", "DPIO Modules"}, + {0x110100, 0, "Performance Counters", "Performance Counters"}, + {0x111000, 0, "Communications Syncrhonization Plus Time and Frequency Test/Measurment", "Communications Syncrhonization Plus Time and Frequency Test/Measurment"}, + {0x112000, 0, "Management Card", "Management Card"}, + {0x118000, 0, "Acquisition/Signal Processing Controller", "Other Data Acquisition/Signal Processing Controller"}, + {0x000000, 0, 0}}; const char *pci_get_info(unsigned int classcode, unsigned int progif) { pci_info_t *p = pci_info; const char *s = 0; - while(p->code != 0 || p->flag != 0 || p->info != 0) + while (p->code != 0 || p->flag != 0 || p->info != 0) { unsigned long code = classcode; - if(p->flag == 0) + if (p->flag == 0) { code <<= 8; code |= progif & 0xFF; } - if(p->code == code) + if (p->code == code) { - if(p->flag == 0) + if (p->flag == 0) { return p->info; } diff --git a/kernel/printk.c b/kernel/printk.c index bfe105a..6a1af0c 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -30,7 +30,7 @@ void switch_printk_screen() char pkbuf[1024]; int printk(const char *fmtstr, ...) { - char *args = (char*)(((char*)&fmtstr)+4); + char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(pkbuf, fmtstr, args); vga_puts(printk_screen_nr, pkbuf, 0x2); return 0; @@ -39,7 +39,7 @@ int printk(const char *fmtstr, ...) int printd(const char *fmtstr, ...) { char *pdbuf = (char *)kmalloc(1024, 0); - char *args = (char*)(((char*)&fmtstr)+4); + char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(pdbuf, fmtstr, args); vga_puts(3, pdbuf, 0x4); kfree(pdbuf); @@ -49,7 +49,7 @@ int printd(const char *fmtstr, ...) char plobuf[1024]; int printlo(unsigned int line, unsigned int offset, const char *fmtstr, ...) { - char *args = (char*)(((char*)&fmtstr)+4); + char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(plobuf, fmtstr, args); vga_dbg_puts(line, offset, plobuf); return 0; diff --git a/kernel/sched.c b/kernel/sched.c index 5d03605..7080735 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -45,22 +45,21 @@ void init_root_tsk() // memset((char*)&root_task, 0, sizeof(root_task)); root_task.preempt_cnt = 0; - root_task.pid = get_next_pid(); - root_task.ppid = 0; - root_task.state = TASK_RUNNING; + root_task.pid = get_next_pid(); + root_task.ppid = 0; + root_task.state = TASK_RUNNING; root_task.weight = TASK_INIT_WEIGHT; strcpy(root_task.name, "root_task"); INIT_LIST_HEAD(&root_task.list); - // TODO //for(i=0; icr3); tss.esp0 = current->esp0; } -void context_switch(task_union * prev, task_union * next) +void context_switch(task_union *prev, task_union *next) { unsigned long eax, ebx, ecx, edx, esi, edi; asm volatile( - "pushfl;" - "pushl %%ebp;" - "movl %%esp,%[prev_esp];" - "movl %[next_esp],%%esp;" - "movl $1f,%[prev_eip];" - "pushl %[next_eip];" - "jmp switch_to;" - "1:" - "popl %%ebp;" - "popfl;" - : [prev_esp] "=m" (prev->esp), - [prev_eip] "=m" (prev->eip), - "=a" (prev),"=b" (ebx), "=c" (ecx), - "=d" (edx), "=S" (esi), "=D" (edi) - : [next_esp] "m"(next->esp), - [next_eip] "m"(next->eip), - [prev] "a" (prev), - [next] "d" (next) - : "memory" - ); + "pushfl;" + "pushl %%ebp;" + "movl %%esp,%[prev_esp];" + "movl %[next_esp],%%esp;" + "movl $1f,%[prev_eip];" + "pushl %[next_eip];" + "jmp switch_to;" + "1:" + "popl %%ebp;" + "popfl;" + : [prev_esp] "=m"(prev->esp), + [prev_eip] "=m"(prev->eip), + "=a"(prev), "=b"(ebx), "=c"(ecx), + "=d"(edx), "=S"(esi), "=D"(edi) + : [next_esp] "m"(next->esp), + [next_eip] "m"(next->eip), + [prev] "a"(prev), + [next] "d"(next) + : "memory"); } task_union *find_task(pid_t pid) { task_union *p = 0; - list_head_t *pos = 0, *tmp=0; + list_head_t *pos = 0, *tmp = 0; unsigned long iflags; irq_save(iflags); list_for_each_safe(pos, tmp, &root_task.list) { p = list_entry(pos, task_union, list); - if(p->pid == pid) + if (p->pid == pid) break; } irq_restore(iflags); @@ -148,7 +144,7 @@ static const char *task_state(unsigned int state) "EXITING", }; - if(state >= TASK_END) + if (state >= TASK_END) state = TASK_UNUSED; return s[state]; @@ -159,7 +155,7 @@ unsigned long schedule() static turn = 0; task_union *sel = 0; task_union *p = 0; - list_head_t *pos = 0, *t=0; + list_head_t *pos = 0, *t = 0; unsigned long iflags; irq_save(iflags); @@ -168,17 +164,16 @@ unsigned long schedule() { p = list_entry(pos, task_union, list); - printl(MPL_ROOT+p->pid, "task:%d [%08x] state %s cnt %u", p->pid, p, task_state(p->state), p->cnt); + printl(MPL_ROOT + p->pid, "task:%d [%08x] state %s cnt %u", p->pid, p, task_state(p->state), p->cnt); - if(p->state != TASK_RUNNING) + if (p->state != TASK_RUNNING) { continue; } - printd("%08x weight %d\n", p, p->weight); + printd("%08x %s weight %d\n", p, p->name, p->weight); - - if(sel == 0 && p->weight != turn) + if (sel == 0 && p->weight != turn) { p->weight = turn; sel = p; @@ -186,18 +181,16 @@ unsigned long schedule() } irq_restore(iflags); - - if(sel == 0) + if (sel == 0) { sel = &root_task; - turn ++; + turn++; } - task_union *prev = current; task_union *next = sel; - if(prev != next) + if (prev != next) { context_switch(prev, next); } @@ -206,5 +199,5 @@ unsigned long schedule() void debug_sched() { task_union *p = list_entry(current->list.next, task_union, list); - p->state = (p->state == TASK_RUNNING) ? TASK_WAIT: TASK_RUNNING; + p->state = (p->state == TASK_RUNNING) ? TASK_WAIT : TASK_RUNNING; } diff --git a/kernel/semaphore.c b/kernel/semaphore.c index 8f80703..f817464 100644 --- a/kernel/semaphore.c +++ b/kernel/semaphore.c @@ -6,8 +6,8 @@ * Description: none * ------------------------------------------------------------------------ */ -#include -#include +#include +#include typedef struct semaphore_waiter { @@ -16,17 +16,16 @@ typedef struct semaphore_waiter int up; } semaphore_waiter_t; -#define SEMAPHORE_WAITER_INITIALIZER(name, task) \ -{ \ - .list = LIST_HEAD_INIT((name).list), \ - .task = task, \ - .up = 0 \ -} +#define SEMAPHORE_WAITER_INITIALIZER(name, task) \ + { \ + .list = LIST_HEAD_INIT((name).list), \ + .task = task, \ + .up = 0 \ + } -#define DECLARE_SEMAPHORE_WAITER(name, task) \ +#define DECLARE_SEMAPHORE_WAITER(name, task) \ semaphore_waiter_t name = SEMAPHORE_WAITER_INITIALIZER(name, task) - void __down(semaphore_t *s) { task_union *task = current; @@ -41,8 +40,8 @@ void __down(semaphore_t *s) schedule(); disable_irq(); - if(waiter.up) - ;//break; + if (waiter.up) + ; //break; } } @@ -52,9 +51,9 @@ void down(semaphore_t *s) irq_save(iflags); - if(likely(s->cnt>0)) + if (likely(s->cnt > 0)) { - s->cnt --; + s->cnt--; } else { @@ -73,15 +72,14 @@ void __up(semaphore_t *s) waiter->task->state = TASK_RUNNING; } - void up(semaphore_t *s) { unsigned long iflags; irq_save(iflags); - if(likely(list_empty(&s->wait_list))) + if (likely(list_empty(&s->wait_list))) { - s->cnt ++; + s->cnt++; } else { diff --git a/kernel/setup.c b/kernel/setup.c index 488d080..04a0bf3 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -36,7 +36,7 @@ extern void vga_init(); #define HZ 10 #define CLOCK_TICK_RATE 1193180 -#define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) +#define LATCH ((CLOCK_TICK_RATE + HZ / 2) / HZ) void setup_i8253() { @@ -46,12 +46,12 @@ void setup_i8253() } #define VERSION "0.3.1" -const char *version = - "Kernel version " - VERSION - " @ " - BUILDER - " ["__DATE__ " " __TIME__ "]" +const char *version = + "Kernel version " VERSION + " @ " BUILDER + " ["__DATE__ + " " __TIME__ "]" + "\n"; void setup_kernel() @@ -70,10 +70,8 @@ void setup_kernel() setup_i8253(); - set_tss(); - setup_sysc(); cnsl_init(); @@ -102,5 +100,5 @@ void setup_kernel() //vga_puts(0, version, 0x2F); printk(version); - switch_printk_screen(); + //switch_printk_screen(); } diff --git a/kernel/syscall.c b/kernel/syscall.c index 06b620c..a1e4108 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -25,18 +25,18 @@ unsigned long sysc_handler_table[SYSC_NUM]; void setup_sysc() { - wrmsr(MSR_SYSENTER_CS, SELECTOR_KRNL_CS, 0); - wrmsr(MSR_SYSENTER_EIP, syscall_entry, 0); - wrmsr(MSR_SYSENTER_ESP, &(tss.esp0), 0); + wrmsr(MSR_SYSENTER_CS, SELECTOR_KRNL_CS, 0); + wrmsr(MSR_SYSENTER_EIP, syscall_entry, 0); + wrmsr(MSR_SYSENTER_ESP, &(tss.esp0), 0); init_sysc_handler_table(); } - int sysc_none() { int sysc_nr; - asm("":"=a"(sysc_nr)); + asm("" + : "=a"(sysc_nr)); printk("unsupport syscall:%d\n", sysc_nr); return 0; @@ -50,7 +50,7 @@ int sysc_pause(unsigned long tick) int sysc_test() { - static unsigned int cnt=0; + static unsigned int cnt = 0; current->cnt++; printl(MPL_TEST, "systest cnt %u current %08x cnt %u ", cnt++, current, cnt); @@ -60,7 +60,7 @@ int sysc_test() int sysc_debug(unsigned int v) { - static unsigned int cnt=0; + static unsigned int cnt = 0; printl(MPL_DEBUG, "task debug syscall %u value %08x", cnt++, v); return 0; } @@ -68,36 +68,36 @@ int sysc_debug(unsigned int v) void init_sysc_handler_table() { int i; - for(i=0; iseg.DPL = 3; - pdesc = gdt+INDEX_UDATA; + pdesc = gdt + INDEX_UDATA; pdesc->seg.DPL = 3; } void setup_idt() { - *((unsigned short *)idtr) = NIDT*sizeof(Gate); - *((unsigned long *)(idtr+2)) = (unsigned long)idt; + *((unsigned short *)idtr) = NIDT * sizeof(Gate); + *((unsigned long *)(idtr + 2)) = (unsigned long)idt; lidt(); } - - void setup_gate() -{ int i; +{ + int i; set_sys_int(0x00, TRAP_GATE, PRIVILEGE_KRNL, DivideError); set_sys_int(0x01, TRAP_GATE, PRIVILEGE_KRNL, Debug); - set_sys_int(0x02, INTR_GATE, PRIVILEGE_KRNL, NMI); - set_sys_int(0x03, TRAP_GATE, PRIVILEGE_USER, BreakPoint); - set_sys_int(0x04, TRAP_GATE, PRIVILEGE_USER, OverFlow); - set_sys_int(0x05, TRAP_GATE, PRIVILEGE_USER, BoundsCheck); - set_sys_int(0x06, TRAP_GATE, PRIVILEGE_KRNL, InvalidOpcode); - set_sys_int(0x07, TRAP_GATE, PRIVILEGE_KRNL, DeviceNotAvailable); - set_sys_int(0x08, TRAP_GATE, PRIVILEGE_KRNL, DoubleFault); - set_sys_int(0x09, TRAP_GATE, PRIVILEGE_KRNL, CoprocSegOverRun); - set_sys_int(0x0A, TRAP_GATE, PRIVILEGE_KRNL, InvalidTss); - set_sys_int(0x0B, TRAP_GATE, PRIVILEGE_KRNL, SegNotPresent); - set_sys_int(0x0C, TRAP_GATE, PRIVILEGE_KRNL, StackFault); - set_sys_int(0x0D, TRAP_GATE, PRIVILEGE_KRNL, GeneralProtection); - set_sys_int(0x0E, TRAP_GATE, PRIVILEGE_KRNL, PageFault); + set_sys_int(0x02, INTR_GATE, PRIVILEGE_KRNL, NMI); + set_sys_int(0x03, TRAP_GATE, PRIVILEGE_USER, BreakPoint); + set_sys_int(0x04, TRAP_GATE, PRIVILEGE_USER, OverFlow); + set_sys_int(0x05, TRAP_GATE, PRIVILEGE_USER, BoundsCheck); + set_sys_int(0x06, TRAP_GATE, PRIVILEGE_KRNL, InvalidOpcode); + set_sys_int(0x07, TRAP_GATE, PRIVILEGE_KRNL, DeviceNotAvailable); + set_sys_int(0x08, TRAP_GATE, PRIVILEGE_KRNL, DoubleFault); + set_sys_int(0x09, TRAP_GATE, PRIVILEGE_KRNL, CoprocSegOverRun); + set_sys_int(0x0A, TRAP_GATE, PRIVILEGE_KRNL, InvalidTss); + set_sys_int(0x0B, TRAP_GATE, PRIVILEGE_KRNL, SegNotPresent); + set_sys_int(0x0C, TRAP_GATE, PRIVILEGE_KRNL, StackFault); + set_sys_int(0x0D, TRAP_GATE, PRIVILEGE_KRNL, GeneralProtection); + set_sys_int(0x0E, TRAP_GATE, PRIVILEGE_KRNL, PageFault); set_sys_int(0x10, TRAP_GATE, PRIVILEGE_KRNL, CoprocError); - for(i=0x20; i<256; i++) + for (i = 0x20; i < 256; i++) set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); set_sys_int(0x20, INTR_GATE, PRIVILEGE_KRNL, irq_0x00_handler); - set_sys_int(0x21, INTR_GATE, PRIVILEGE_KRNL, irq_0x01_handler); - set_sys_int(0x22, INTR_GATE, PRIVILEGE_KRNL, irq_0x02_handler); - set_sys_int(0x23, INTR_GATE, PRIVILEGE_KRNL, irq_0x03_handler); - set_sys_int(0x24, INTR_GATE, PRIVILEGE_KRNL, irq_0x04_handler); - set_sys_int(0x25, INTR_GATE, PRIVILEGE_KRNL, irq_0x05_handler); - set_sys_int(0x26, INTR_GATE, PRIVILEGE_KRNL, irq_0x06_handler); - set_sys_int(0x27, INTR_GATE, PRIVILEGE_KRNL, irq_0x07_handler); - set_sys_int(0x28, INTR_GATE, PRIVILEGE_KRNL, irq_0x08_handler); - set_sys_int(0x29, INTR_GATE, PRIVILEGE_KRNL, irq_0x09_handler); - set_sys_int(0x2A, INTR_GATE, PRIVILEGE_KRNL, irq_0x0A_handler); - set_sys_int(0x2B, INTR_GATE, PRIVILEGE_KRNL, irq_0x0B_handler); - set_sys_int(0x2C, INTR_GATE, PRIVILEGE_KRNL, irq_0x0C_handler); - set_sys_int(0x2D, INTR_GATE, PRIVILEGE_KRNL, irq_0x0D_handler); - set_sys_int(0x2E, INTR_GATE, PRIVILEGE_KRNL, irq_0x0E_handler); - set_sys_int(0x2F, INTR_GATE, PRIVILEGE_KRNL, irq_0x0F_handler); + set_sys_int(0x21, INTR_GATE, PRIVILEGE_KRNL, irq_0x01_handler); + set_sys_int(0x22, INTR_GATE, PRIVILEGE_KRNL, irq_0x02_handler); + set_sys_int(0x23, INTR_GATE, PRIVILEGE_KRNL, irq_0x03_handler); + set_sys_int(0x24, INTR_GATE, PRIVILEGE_KRNL, irq_0x04_handler); + set_sys_int(0x25, INTR_GATE, PRIVILEGE_KRNL, irq_0x05_handler); + set_sys_int(0x26, INTR_GATE, PRIVILEGE_KRNL, irq_0x06_handler); + set_sys_int(0x27, INTR_GATE, PRIVILEGE_KRNL, irq_0x07_handler); + set_sys_int(0x28, INTR_GATE, PRIVILEGE_KRNL, irq_0x08_handler); + set_sys_int(0x29, INTR_GATE, PRIVILEGE_KRNL, irq_0x09_handler); + set_sys_int(0x2A, INTR_GATE, PRIVILEGE_KRNL, irq_0x0A_handler); + set_sys_int(0x2B, INTR_GATE, PRIVILEGE_KRNL, irq_0x0B_handler); + set_sys_int(0x2C, INTR_GATE, PRIVILEGE_KRNL, irq_0x0C_handler); + set_sys_int(0x2D, INTR_GATE, PRIVILEGE_KRNL, irq_0x0D_handler); + set_sys_int(0x2E, INTR_GATE, PRIVILEGE_KRNL, irq_0x0E_handler); + set_sys_int(0x2F, INTR_GATE, PRIVILEGE_KRNL, irq_0x0F_handler); } void ide_irq(); -void default_ide_irq_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) +void default_ide_irq_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { //printk("default irq handler %d \n", irq); ide_irq(); } -void default_irq_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) +void default_irq_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { printk("default irq handler %d \n", irq); } @@ -111,48 +110,48 @@ void setup_irqs() init_i8259(); int i; - for(i=0; iesp0 = 0; // delay to init root_task - p->ss0 = SELECTOR_KRNL_DS; - p->ss = SELECTOR_KRNL_DS; - p->gs = SELECTOR_KRNL_DS; - p->fs = SELECTOR_KRNL_DS; - p->es = SELECTOR_KRNL_DS; - p->ds = SELECTOR_KRNL_DS; - p->cs = SELECTOR_KRNL_CS; - p->eflags = 0x1200; - p->iomap_base = sizeof(TSS); + p->esp0 = 0; // delay to init root_task + p->ss0 = SELECTOR_KRNL_DS; + p->ss = SELECTOR_KRNL_DS; + p->gs = SELECTOR_KRNL_DS; + p->fs = SELECTOR_KRNL_DS; + p->es = SELECTOR_KRNL_DS; + p->ds = SELECTOR_KRNL_DS; + p->cs = SELECTOR_KRNL_CS; + p->eflags = 0x1200; + p->iomap_base = sizeof(TSS); set_tss_gate(INDEX_TSS, (u32)p); - asm("ltr %%ax"::"a"((INDEX_TSS<<3)+3)); + asm("ltr %%ax" ::"a"((INDEX_TSS << 3) + 3)); } int sysc_reboot(int mode) @@ -161,7 +160,7 @@ int sysc_reboot(int mode) void do_reboot(); void do_poweroff(); - switch(mode) + switch (mode) { case 0: do_reboot(); @@ -179,14 +178,14 @@ void system_delay() unsigned long flags; irq_save(flags); unsigned int n = system.delay; - while(n--) + while (n--) { unsigned long cr0; - asm("movl %%cr0, %%eax;":"=a"(cr0)); - asm("movl %%eax, %%cr0;"::"a"(cr0)); + asm("movl %%cr0, %%eax;" + : "=a"(cr0)); + asm("movl %%eax, %%cr0;" ::"a"(cr0)); } irq_restore(flags); } - -char gdtr[6],idtr[6]; +char gdtr[6], idtr[6]; diff --git a/kernel/wait.c b/kernel/wait.c index 9278067..9cf47b5 100644 --- a/kernel/wait.c +++ b/kernel/wait.c @@ -46,29 +46,28 @@ void wake_up(wait_queue_head_t *wqh) // no schedule() here. } - -#include +#include DECLARE_WAIT_QUEUE_HEAD(debug_wq); unsigned int debug_global_var = 0; int debug_wait_queue_get() { unsigned int v = 0; - task_union * task = current; + task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&debug_wq, &wait); - while(1) + while (1) { printd("pid %d is going to wait\n", sysc_getpid()); task->state = TASK_WAIT; disable_irq(); v = debug_global_var; - if(debug_global_var != 0) + if (debug_global_var != 0) debug_global_var--; enable_irq(); - if(v != 0) + if (v != 0) break; schedule(); @@ -88,31 +87,30 @@ int debug_wait_queue_put(unsigned int v) wake_up(&debug_wq); } - int sysc_wait(unsigned long pid) { task_union *p = find_task(pid); - if(p == 0) + if (p == 0) return 0; - if(p->state == TASK_EXITING) + if (p->state == TASK_EXITING) return 0; - task_union * task = current; + task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&p->wait, &wait); - while(true) + while (true) { //task->state = TASK_WAIT; - + unsigned long flags; irq_save(flags); unsigned int state = p->state; irq_restore(flags); - if(state == TASK_EXITING) // no need irq_save + if (state == TASK_EXITING) // no need irq_save break; schedule(); diff --git a/lib/fork.c b/lib/fork.c index 6b2696f..2e45f23 100644 --- a/lib/fork.c +++ b/lib/fork.c @@ -13,7 +13,7 @@ #include #include #include -pid_t fork() +pid_t fork() { #if 0 pid_t pid; @@ -26,5 +26,5 @@ pid_t fork() pid = syscall0(SYSC_FORK); return pid; #endif - return (pid_t) syscall0(SYSC_FORK); + return (pid_t)syscall0(SYSC_FORK); } diff --git a/lib/keyboard.c b/lib/keyboard.c index c9043aa..81b3eea 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -18,37 +18,37 @@ #include #include #include -#define EXT_KEY 0x80000000 /* None Print Key */ -#define L_SHIFT_DOWN 0x00000100 -#define R_SHIFT_DOWN 0x00000200 -#define L_CTRL_DOWN 0x00000400 -#define R_CTRL_DOWN 0x00000800 -#define L_ALT_DOWN 0x00001000 -#define R_ALT_DOWN 0x00002000 -#define L_SHIFT_UP (~L_SHIFT_DOWN) -#define R_SHIFT_UP (~R_SHIFT_DOWN) -#define L_CTRL_UP (~L_CTRL_DOWN) -#define R_CTRL_UP (~R_CTRL_DOWN) -#define L_ALT_UP (~L_ALT_DOWN) -#define R_ALT_UP (~R_ALT_DOWN) -#define SET_L_SHIFT_DOWN(key) (key |= L_SHIFT_DOWN) -#define SET_R_SHIFT_DOWN(key) (key |= R_SHIFT_DOWN) -#define SET_L_CTRL_DOWN(key) (key |= L_CTRL_DOWN) -#define SET_R_CTRL_DOWN(key) (key |= R_CTRL_DOWN) -#define SET_L_ALT_DOWN(key) (key |= L_ALT_DOWN) -#define SET_R_ALT_DOWN(key) (key |= R_ALT_DOWN) -#define SET_L_SHIFT_UP(key) (key &= L_SHIFT_UP) -#define SET_R_SHIFT_UP(key) (key &= R_SHIFT_UP) -#define SET_L_CTRL_UP(key) (key &= L_CTRL_UP) -#define SET_R_CTRL_UP(key) (key &= R_CTRL_UP) -#define SET_L_ALT_UP(key) (key &= L_ALT_UP) -#define SET_R_ALT_UP(key) (key &= R_ALT_UP) -#define IS_L_SHIFT_DOWN(key) (key & L_SHIFT_DOWN) -#define IS_R_SHIFT_DOWN(key) (key & R_SHIFT_DOWN) -#define IS_L_CTRL_DOWN(key) (key & L_CTRL_DOWN) -#define IS_R_CTRL_DOWN(key) (key & R_CTRL_DOWN) -#define IS_L_ALT_DOWN(key) (key & L_ALT_DOWN) -#define IS_R_ALT_DOWN(key) (key & R_ALT_DOWN) +#define EXT_KEY 0x80000000 /* None Print Key */ +#define L_SHIFT_DOWN 0x00000100 +#define R_SHIFT_DOWN 0x00000200 +#define L_CTRL_DOWN 0x00000400 +#define R_CTRL_DOWN 0x00000800 +#define L_ALT_DOWN 0x00001000 +#define R_ALT_DOWN 0x00002000 +#define L_SHIFT_UP (~L_SHIFT_DOWN) +#define R_SHIFT_UP (~R_SHIFT_DOWN) +#define L_CTRL_UP (~L_CTRL_DOWN) +#define R_CTRL_UP (~R_CTRL_DOWN) +#define L_ALT_UP (~L_ALT_DOWN) +#define R_ALT_UP (~R_ALT_DOWN) +#define SET_L_SHIFT_DOWN(key) (key |= L_SHIFT_DOWN) +#define SET_R_SHIFT_DOWN(key) (key |= R_SHIFT_DOWN) +#define SET_L_CTRL_DOWN(key) (key |= L_CTRL_DOWN) +#define SET_R_CTRL_DOWN(key) (key |= R_CTRL_DOWN) +#define SET_L_ALT_DOWN(key) (key |= L_ALT_DOWN) +#define SET_R_ALT_DOWN(key) (key |= R_ALT_DOWN) +#define SET_L_SHIFT_UP(key) (key &= L_SHIFT_UP) +#define SET_R_SHIFT_UP(key) (key &= R_SHIFT_UP) +#define SET_L_CTRL_UP(key) (key &= L_CTRL_UP) +#define SET_R_CTRL_UP(key) (key &= R_CTRL_UP) +#define SET_L_ALT_UP(key) (key &= L_ALT_UP) +#define SET_R_ALT_UP(key) (key &= R_ALT_UP) +#define IS_L_SHIFT_DOWN(key) (key & L_SHIFT_DOWN) +#define IS_R_SHIFT_DOWN(key) (key & R_SHIFT_DOWN) +#define IS_L_CTRL_DOWN(key) (key & L_CTRL_DOWN) +#define IS_R_CTRL_DOWN(key) (key & R_CTRL_DOWN) +#define IS_L_ALT_DOWN(key) (key & L_ALT_DOWN) +#define IS_R_ALT_DOWN(key) (key & R_ALT_DOWN) const unsigned char kbdCharTable[]={0,0, '1','2','3','4','5','6','7','8','9','0','-','=','\b',0, @@ -66,20 +66,20 @@ const unsigned char kbdShiftCharTable[]={0,0, }; /* Make Code */ -#define MC_BACKSPACE 0x0E -#define MC_CTRL 0x1D -#define MC_L_SHIFT 0x2A -#define MC_R_SHIFT 0x36 -#define MC_ALT 0x38 -#define MC_CAPSLOCK 0x3A -#define MC_DELETE 0x53 -#define BC_BACKSPACE (0x80 | MC_BACKSPACE) -#define BC_CTRL (0x80 | MC_CTRL) -#define BC_L_SHIFT (0x80 | MC_L_SHIFT) -#define BC_R_SHIFT (0x80 | MC_R_SHIFT) -#define BC_ALT (0x80 | MC_ALT) -#define BC_DELETE (0x80 | MC_DELETE) -#define BC_CAPSLOCK (0x80 | MC_CAPSLOCK) +#define MC_BACKSPACE 0x0E +#define MC_CTRL 0x1D +#define MC_L_SHIFT 0x2A +#define MC_R_SHIFT 0x36 +#define MC_ALT 0x38 +#define MC_CAPSLOCK 0x3A +#define MC_DELETE 0x53 +#define BC_BACKSPACE (0x80 | MC_BACKSPACE) +#define BC_CTRL (0x80 | MC_CTRL) +#define BC_L_SHIFT (0x80 | MC_L_SHIFT) +#define BC_R_SHIFT (0x80 | MC_R_SHIFT) +#define BC_ALT (0x80 | MC_ALT) +#define BC_DELETE (0x80 | MC_DELETE) +#define BC_CAPSLOCK (0x80 | MC_CAPSLOCK) static unsigned char E0Flag = 0; diff --git a/lib/lib.c b/lib/lib.c index c659370..afda33f 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -14,13 +14,13 @@ int isdigit(char c) { - return ('0'<=c && c<='9'); + return ('0' <= c && c <= '9'); } int atoi(const char *s) { - int i=0; - while(isdigit(*s)) + int i = 0; + while (isdigit(*s)) { i *= 10; i += (*s++ - '0'); @@ -31,12 +31,12 @@ int atoi(const char *s) void reboot() { - syscall1(SYSC_REBOOT,0); + syscall1(SYSC_REBOOT, 0); } void poweroff() { - syscall1(SYSC_REBOOT,1); + syscall1(SYSC_REBOOT, 1); } int systest() diff --git a/lib/read.c b/lib/read.c index 419db62..35326ae 100644 --- a/lib/read.c +++ b/lib/read.c @@ -13,5 +13,5 @@ #include ssize_t read(int fd, void *buf, size_t count) { - return (ssize_t) syscall3(SYSC_READ, fd, buf, count); + return (ssize_t)syscall3(SYSC_READ, fd, buf, count); } diff --git a/lib/string.c b/lib/string.c index ad82cd8..a7d0daa 100644 --- a/lib/string.c +++ b/lib/string.c @@ -8,14 +8,16 @@ char *strcpy(char *dest, const char *src) { char *p = dest; - while((*dest++ = *src++)); + while ((*dest++ = *src++)) + ; return p; } size_t strlen(const char *str) { - int i=0; - while(*str++) i++; + int i = 0; + while (*str++) + i++; return i; } @@ -25,7 +27,7 @@ int strcmp(const char *a, const char *b) while (*a || *b) { delta = *a++ - *b++; - if(delta != 0) + if (delta != 0) return delta; } return 0; @@ -35,18 +37,18 @@ int strncmp(const char *a, const char *b, size_t count) { unsigned char c1, c2; int delta; - while(count) + while (count) { c1 = *a++; c2 = *b++; - delta = c1-c2; - if(delta != 0) + delta = c1 - c2; + if (delta != 0) return delta; - if(c1 == 0) + if (c1 == 0) break; - + count--; } @@ -56,30 +58,38 @@ int strncmp(const char *a, const char *b, size_t count) char *strcat(char *dest, const char *src) { char *tmp = dest; - while(*dest) dest++; - while((*dest++ = *src++) != '\0'); + while (*dest) + dest++; + while ((*dest++ = *src++) != '\0') + ; return tmp; } void *memcpy(void *dest, const void *src, size_t size) { - char *d = (char *) dest; - char *s = (char *) src; - while(size-->0) {*d = *s;d++;s++;} + char *d = (char *)dest; + char *s = (char *)src; + while (size-- > 0) + { + *d = *s; + d++; + s++; + } return dest; } void memset(void *dest, char ch, size_t size) { - char *d = (char *) dest; - while(size--) *d++ = ch; + char *d = (char *)dest; + while (size--) + *d++ = ch; } int memcmp(const void *a, const void *b, size_t count) { const unsigned char *sa, *sb; int delta = 0; - for(sa=a, sb=b; count>0; ++sa, ++sb, --count) - if((delta = *sa - *sb) != 0) + for (sa = a, sb = b; count > 0; ++sa, ++sb, --count) + if ((delta = *sa - *sb) != 0) break; return delta; } @@ -88,13 +98,13 @@ char *strstr(const char *a, const char *b) { size_t la, lb; lb = strlen(b); - if(lb == 0) - return (char *) a; + if (lb == 0) + return (char *)a; la = strlen(a); - while(la >= lb) + while (la >= lb) { la--; - if(memcmp(a, b, lb) == 0) + if (memcmp(a, b, lb) == 0) return (char *)a; a++; } diff --git a/lib/syscall.c b/lib/syscall.c index c4a2276..1f86e73 100644 --- a/lib/syscall.c +++ b/lib/syscall.c @@ -9,47 +9,57 @@ // "movl $1f, %%edi;" // -#define SYSENTER_ASM \ - "pushl $1f;" \ - "pushl %%ecx;" \ - "pushl %%edx;" \ - "pushl %%ebp;" \ - "movl %%esp,%%ebp;" \ - "sysenter;" \ - "1:" +#define SYSENTER_ASM \ + "pushl $1f;" \ + "pushl %%ecx;" \ + "pushl %%edx;" \ + "pushl %%ebp;" \ + "movl %%esp,%%ebp;" \ + "sysenter;" \ + "1:" -static int __volatile__ __syscall0(int nr) +static int __volatile__ __syscall0(int nr) { int __sysc_ret__ = 0; - asm(SYSENTER_ASM:"=a"(__sysc_ret__):"a"(nr)); + asm(SYSENTER_ASM + : "=a"(__sysc_ret__) + : "a"(nr)); return __sysc_ret__; } static int __volatile__ __syscall1(int nr, unsigned long a) { int __sysc_ret__ = 0; - asm(SYSENTER_ASM:"=a"(__sysc_ret__):"a"(nr), "b"(a)); + asm(SYSENTER_ASM + : "=a"(__sysc_ret__) + : "a"(nr), "b"(a)); return __sysc_ret__; } static int __volatile__ __syscall2(int nr, unsigned long a, unsigned long b) { int __sysc_ret__ = 0; - asm(SYSENTER_ASM:"=a"(__sysc_ret__):"a"(nr), "b"(a), "c"(b)); + asm(SYSENTER_ASM + : "=a"(__sysc_ret__) + : "a"(nr), "b"(a), "c"(b)); return __sysc_ret__; } static int __volatile__ __syscall3(int nr, unsigned long a, unsigned long b, unsigned long c) { int __sysc_ret__ = 0; - asm(SYSENTER_ASM:"=a"(__sysc_ret__):"a"(nr), "b"(a), "c"(b), "d"(c)); + asm(SYSENTER_ASM + : "=a"(__sysc_ret__) + : "a"(nr), "b"(a), "c"(b), "d"(c)); return __sysc_ret__; } static int __volatile__ __syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d) { int __sysc_ret__ = 0; - asm(SYSENTER_ASM:"=a"(__sysc_ret__):"a"(nr), "b"(a), "c"(b), "d"(c), "S"(d)); + asm(SYSENTER_ASM + : "=a"(__sysc_ret__) + : "a"(nr), "b"(a), "c"(b), "d"(c), "S"(d)); return __sysc_ret__; } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d5c057e..870829c 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -10,14 +10,15 @@ char *itoa(char *s, int n); char *itou(char *s, unsigned int n); char *itox(char *s, unsigned int n); -enum { +enum +{ ALIGN_RIGHT, ALIGN_LEFT }; int write_buf(char *buf, const char *str, char fillch, int charcnt, int align) { - if(str == 0) + if (str == 0) return 0; int len = strlen(str); @@ -26,7 +27,7 @@ int write_buf(char *buf, const char *str, char fillch, int charcnt, int align) int s_pos = 0; int c_pos = len; - if(ALIGN_RIGHT == align) + if (ALIGN_RIGHT == align) { s_pos = delta_char_cnt > 0 ? delta_char_cnt : 0; c_pos = 0; @@ -35,7 +36,7 @@ int write_buf(char *buf, const char *str, char fillch, int charcnt, int align) strcpy(buf + s_pos, str); int i = 0; - for(i=0; i= '0' && *(fmt) <= '9') + while (*(fmt) >= '0' && *(fmt) <= '9') { char_cnt += *(fmt) - '0'; char_cnt *= 10; @@ -82,24 +83,24 @@ int vsprintf(char *buf, const char *fmt, char *args) } char_cnt /= 10; - switch(*fmt) + switch (*fmt) { case 'c': *p++ = *args; break; case 'd': - itoa(tmp, *((int*)args)); + itoa(tmp, *((int *)args)); p += write_buf(p, tmp, char_fill, char_cnt, align); break; case 's': - p += write_buf(p, (const char *)*((unsigned int *) args), char_fill, char_cnt, align); + p += write_buf(p, (const char *)*((unsigned int *)args), char_fill, char_cnt, align); break; case 'u': - itou(tmp, *((unsigned int*)args)); + itou(tmp, *((unsigned int *)args)); p += write_buf(p, tmp, char_fill, char_cnt, align); break; case 'x': - itox(tmp, *((unsigned int *) args)); + itox(tmp, *((unsigned int *)args)); p += write_buf(p, tmp, char_fill, char_cnt, align); break; default: @@ -124,7 +125,7 @@ char *itoa(char *s, int n) int i = 0; char *p = 0; - if( n & 0x80000000 ) + if (n & 0x80000000) { n = ~n + 1; *s++ = '-'; @@ -136,11 +137,11 @@ char *itoa(char *s, int n) { *p++ = (n % 10) + '0'; n /= 10; - }while(n); + } while (n); *p-- = 0; - while(s < p) + while (s < p) { swap_char(s, p); s++; @@ -148,7 +149,6 @@ char *itoa(char *s, int n) } } - char *itou(char *s, unsigned int n) { char c; @@ -158,11 +158,11 @@ char *itou(char *s, unsigned int n) { *p++ = (n % 10) + '0'; n /= 10; - }while(n); + } while (n); *p-- = 0; - while(s < p) + while (s < p) { swap_char(s, p); s++; @@ -177,11 +177,11 @@ char *itox(char *s, unsigned int n) int i; bool flag = false; - for(i=28; i>=0; i-=4) + for (i = 28; i >= 0; i -= 4) { - ch = (n>>i) & 0x0F; + ch = (n >> i) & 0x0F; - if(ch>=0 && ch<=9) + if (ch >= 0 && ch <= 9) { ch += '0'; } @@ -191,14 +191,14 @@ char *itox(char *s, unsigned int n) ch += 'A'; } - if(ch != '0') + if (ch != '0') flag = true; - if(flag || ch != '0') + if (flag || ch != '0') *p++ = ch; } - if(s == p) + if (s == p) *p++ = '0'; *p = 0; diff --git a/lib/write.c b/lib/write.c index 9ab3b1d..09d2b27 100644 --- a/lib/write.c +++ b/lib/write.c @@ -22,7 +22,7 @@ int write(int fd, const char *buf, unsigned long size) //sysenter(0); //syscall3(0, fd, buf, size); //asm("nop;nop;nop;"); - + syscall3(SYSC_WRITE, fd, buf, size); return size; diff --git a/mm/buddy.c b/mm/buddy.c index 092f2ae..7869f30 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -12,8 +12,8 @@ struct buddy_system { - page_t *page_map; - page_t *page_map_end; + page_t *page_map; + page_t *page_map_end; free_area_t free_area[MAX_ORDER]; }; @@ -21,7 +21,7 @@ struct buddy_system buddy_system; int buddy_is_free(page_t *page, unsigned int order) { - if(PagePrivate(page) && page->private == order) + if (PagePrivate(page) && page->private == order) return 1; return 0; @@ -33,7 +33,7 @@ page_t *_va2page(unsigned long addr) assert(page >= buddy_system.page_map); //assert(page < buddy_system.page_map_end); - if(page >= buddy_system.page_map_end) + if (page >= buddy_system.page_map_end) { printk("buddy_system.page_map %08x buddy_system.page_map_end %08x\n", buddy_system.page_map, buddy_system.page_map_end); printk("error %s page %08x addr %08x\n", __func__, page, addr); @@ -44,29 +44,29 @@ page_t *_va2page(unsigned long addr) } page_t *_pa2page(unsigned long paddr) { - unsigned long vaddr = (unsigned long) pa2va(paddr); + unsigned long vaddr = (unsigned long)pa2va(paddr); //printk("%s paddr %08x vaddr %08x\n", __func__, paddr, vaddr); return va2page(vaddr); } void *page2va(page_t *page) { - return pfn2va((page) - buddy_system.page_map); + return pfn2va((page)-buddy_system.page_map); } page_t *__alloc_pages(unsigned int order) { // page_t *page = 0; - page_t *buddy= 0; + page_t *buddy = 0; free_area_t *area; unsigned long size; unsigned int select_order; unsigned int i; - for(select_order=order; select_orderfree_list))) + if (!list_empty(&(area->free_list))) { goto found; } @@ -81,21 +81,21 @@ found: page->private = 0; area->free_count--; - while(select_order > order) + while (select_order > order) { area--; select_order--; size = 1UL << select_order; buddy = page + size; - list_add(&(buddy->lru), &(area->free_list)); + list_add(&(buddy->lru), &(area->free_list)); area->free_count++; buddy->private = select_order; SetPagePrivate(buddy); } // - for(i=0; i<(1UL<head_page = page; @@ -117,23 +117,24 @@ unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) page_t *page = __alloc_pages(order); irq_restore(flags); - unsigned long addr = (unsigned long) page2va(page); + unsigned long addr = (unsigned long)page2va(page); return addr; } void __free_pages(page_t *page, unsigned int order) { - if(order > MAX_ORDER) - return ; + if (order > MAX_ORDER) + return; page_t *buddy = 0; - page_t *base = buddy_system.page_map; - unsigned long page_inx = page - base; - while(order < (MAX_ORDER-1)) + page_t *base = buddy_system.page_map; + unsigned long page_inx = page - base; + while (order < (MAX_ORDER - 1)) { unsigned long buddy_inx = page_inx ^ (1UL << order); buddy = base + buddy_inx; - if(!buddy_is_free(buddy, order)) { + if (!buddy_is_free(buddy, order)) + { break; } @@ -148,18 +149,17 @@ void __free_pages(page_t *page, unsigned int order) order++; } - page_t *p = base + page_inx; + page_t *p = base + page_inx; p->private = order; - p->index = page_inx; + p->index = page_inx; SetPagePrivate(p); list_add(&(p->lru), &(buddy_system.free_area[order].free_list)); buddy_system.free_area[order].free_count++; } - void free_pages(unsigned long addr) { - if(!valid_va(addr)) + if (!valid_va(addr)) { BUG_ON(!valid_va(addr)); } @@ -175,11 +175,11 @@ void free_pages(unsigned long addr) void dump_buddy_system() { unsigned long i; - for(i=0; iprivate = 0; - page->index = i; + page->index = i; INIT_LIST_HEAD(&(page->lru)); ClearPagePrivate(page); } // get free pages from bootmem - for(i=0; i #include - extern char kernel_begin, kernel_end; -extern char etext,edata,end; +extern char etext, edata, end; extern void init_buddy_system(); extern void init_slub_system(); static void e820_print_type(unsigned long type) { - switch (type) { + switch (type) + { case E820_RAM: printk("usable"); break; @@ -49,19 +49,18 @@ static void e820_print_type(unsigned long type) default: printk("type %x", type); break; - } + } } - void e820_print_map() { - unsigned int i=0; + unsigned int i = 0; - for(i=0; iaddr, p->addr + p->size - 1, p->size, p->size>>10, p->size>>20); + printk(" [%02d] 0x%08x - 0x%08x size %- 10d %8dKB %5dMB ", i, p->addr, p->addr + p->size - 1, p->size, p->size >> 10, p->size >> 20); e820_print_type(p->type); @@ -69,18 +68,18 @@ void e820_print_map() } } -typedef struct bootmem_data { +typedef struct bootmem_data +{ unsigned long min_pfn; unsigned long max_pfn; - unsigned long last_offset; // offset to pfn2pa(this->min_pfn); + unsigned long last_offset; // offset to pfn2pa(this->min_pfn); unsigned long last_hit_pfn; // last hit index in bitmap - void *bitmap; + void *bitmap; unsigned long mapsize; } bootmem_data_t; - bootmem_data_t bootmem_data; unsigned long bootmem_max_pfn() @@ -95,25 +94,25 @@ unsigned long bootmem_page_state(unsigned long pfn) void e820_init_bootmem_data() { - unsigned int i=0; + unsigned int i = 0; memset(&bootmem_data, 0, sizeof(bootmem_data)); - bootmem_data.min_pfn = ~0UL; + bootmem_data.min_pfn = ~0UL; unsigned long bgn_pfn; unsigned long end_pfn; - for(i=0; itype != E820_RAM) + if (p->type != E820_RAM) continue; bgn_pfn = PFN_UP(p->addr); end_pfn = PFN_DW(p->addr + p->size); - if(bootmem_data.max_pfn < end_pfn) + if (bootmem_data.max_pfn < end_pfn) bootmem_data.max_pfn = end_pfn; } @@ -121,7 +120,7 @@ void e820_init_bootmem_data() // limit max_pfn unsigned long max_support_pfn = PFN_DW(MAX_SUPT_PHYMM_SIZE); - if(bootmem_data.max_pfn > max_support_pfn) + if (bootmem_data.max_pfn > max_support_pfn) { bootmem_data.max_pfn = max_support_pfn; } @@ -129,32 +128,32 @@ void e820_init_bootmem_data() void register_bootmem_pages() { - unsigned int i=0; - unsigned int j=0; + unsigned int i = 0; + unsigned int j = 0; - for(i=0; itype != E820_RAM) + if (p->type != E820_RAM) continue; unsigned long bgn_pfn = PFN_UP(p->addr); unsigned long end_pfn = PFN_DW(p->addr + p->size); - for(j=bgn_pfn; jmax_pfn; // start from last position - if(pbd->last_hit_pfn > bgn_pfn) + if (pbd->last_hit_pfn > bgn_pfn) { fallback = bgn_pfn + 1; - bgn_pfn = ALIGN(pbd->last_hit_pfn, step); + bgn_pfn = ALIGN(pbd->last_hit_pfn, step); } - while(1) + while (1) { int merge; void *region; unsigned long i, search_end_pfn; unsigned long start_off, end_off; -find_block: + find_block: bgn_pfn = find_next_zero_bit(pbd->bitmap, end_pfn, bgn_pfn); bgn_pfn = ALIGN(bgn_pfn, step); search_end_pfn = bgn_pfn + PFN_UP(size); - if(bgn_pfn >= end_pfn || search_end_pfn > end_pfn) + if (bgn_pfn >= end_pfn || search_end_pfn > end_pfn) break; - for(i=bgn_pfn; ilast_offset & (PAGE_SIZE - 1) && PFN_DW(pbd->last_offset) + 1 == bgn_pfn) + if (pbd->last_offset & (PAGE_SIZE - 1) && PFN_DW(pbd->last_offset) + 1 == bgn_pfn) start_off = ALIGN(pbd->last_offset, align); else start_off = pfn2pa(bgn_pfn); - merge = PFN_DW(start_off) < bgn_pfn; + merge = PFN_DW(start_off) < bgn_pfn; end_off = start_off + size; - pbd->last_offset = end_off; + pbd->last_offset = end_off; pbd->last_hit_pfn = PFN_UP(end_off); reserve_bootmem(PFN_DW(start_off) + merge, PFN_UP(end_off)); - + region = pa2va(start_off); memset(region, 0, size); @@ -279,9 +279,9 @@ find_block: return region; } - if(fallback) + if (fallback) { - bgn_pfn = ALIGN(fallback-1, step); + bgn_pfn = ALIGN(fallback - 1, step); fallback = 0; goto find_block; } @@ -289,13 +289,12 @@ find_block: return 0; } - -pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); -pte_t __initdata init_pgt[PTECNT_PER_PAGE*BOOT_INIT_PAGETBL_CNT] __attribute__((__aligned__(PAGE_SIZE))); +pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); +pte_t __initdata init_pgt[PTECNT_PER_PAGE * BOOT_INIT_PAGETBL_CNT] __attribute__((__aligned__(PAGE_SIZE))); void set_page_shared(void *x) { - unsigned long addr = (unsigned long) x; + unsigned long addr = (unsigned long)x; addr = PAGE_ALIGN(addr); unsigned int npd = get_npd(addr); init_pgd[npd] |= PAGE_US; @@ -312,14 +311,14 @@ void init_paging() unsigned long pfn = 0; pte_t *pte = 0; unsigned long pgtb_addr = 0; - for(pfn=pa2pfn(BOOT_INIT_PAGETBL_CNT<<22); pfn= PAGE_OFFSET) + if ((unsigned long)addr >= PAGE_OFFSET) { panic("%s invalid addr", __func__); } @@ -60,13 +59,13 @@ void do_wp_page(void *addr) pte_t *page_tbl = pa2va(PAGE_ALIGN(page_dir[npde])); unsigned long wp_pa_addr = PAGE_ALIGN(page_tbl[npte]); - + page_t *page = pa2page(wp_pa_addr); - if(page->count > 0) + if (page->count > 0) { - page->count --; + page->count--; unsigned long flags = PAGE_FLAGS(page_tbl[npte]); - unsigned long wp_va_addr = (unsigned long) pa2va(wp_pa_addr); + unsigned long wp_va_addr = (unsigned long)pa2va(wp_pa_addr); unsigned long newtbl = alloc_one_page(0); assert(newtbl != 0); diff --git a/mm/slub.c b/mm/slub.c index 820ce8e..6c0e4a0 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -13,12 +13,11 @@ list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); - -#define SLUB_MIN_SHIFT 5 -#define SLUB_MAX_SHIFT 16 -#define SLUB_INIT_CACHE_SIZE ((SLUB_MAX_SHIFT) - (SLUB_MIN_SHIFT)) -#define KMALLOC_MIN_SIZE (1UL<<(SLUB_MIN_SHIFT)) -#define KMALLOC_MIN_ALIGN (1UL<<(SLUB_MIN_SHIFT)) +#define SLUB_MIN_SHIFT 5 +#define SLUB_MAX_SHIFT 16 +#define SLUB_INIT_CACHE_SIZE ((SLUB_MAX_SHIFT) - (SLUB_MIN_SHIFT)) +#define KMALLOC_MIN_SIZE (1UL << (SLUB_MIN_SHIFT)) +#define KMALLOC_MIN_ALIGN (1UL << (SLUB_MIN_SHIFT)) static kmem_cache_t kmalloc_caches[SLUB_INIT_CACHE_SIZE]; @@ -26,7 +25,7 @@ static bool calculate_params(kmem_cache_t *cache) { // calculate size unsigned long size = cache->objsize; - unsigned long align= cache->align; + unsigned long align = cache->align; align = KMALLOC_MIN_ALIGN > align ? KMALLOC_MIN_ALIGN : align; size = ALIGN(size, align); size = ALIGN(size, sizeof(void *)); @@ -34,31 +33,29 @@ static bool calculate_params(kmem_cache_t *cache) // calculate order unsigned long order; - for(order=1; ordersize >= 4) + if ((PAGE_SIZE << order) / cache->size >= 4) { cache->order = order; break; } } - if(0 == cache->order) + if (0 == cache->order) { printk("can not find a valid order\n"); return false; } - cache->objects = (PAGE_SIZE << cache->order) / cache->size; - if(0 == cache->objects) + if (0 == cache->objects) return false; return true; } - static bool kmem_cache_init(kmem_cache_t *cache, const char *name, size_t size, @@ -67,17 +64,16 @@ static bool kmem_cache_init(kmem_cache_t *cache, memset(cache, 0, sizeof(kmem_cache_t)); - cache->name = name; - cache->objsize = size; - cache->align = align; - cache->page = 0; - cache->partial_cnt = 0; + cache->name = name; + cache->objsize = size; + cache->align = align; + cache->page = 0; + cache->partial_cnt = 0; INIT_LIST_HEAD(&(cache->partial)); - if(!calculate_params(cache)) + if (!calculate_params(cache)) goto err; - return true; err: panic("kmem_cache_init can not create cache\n"); @@ -86,7 +82,7 @@ err: static page_t *get_partial(kmem_cache_t *cache, gfp_t gfpflags) { - if(list_empty(&cache->partial)) + if (list_empty(&cache->partial)) return 0; list_head_t *p = cache->partial.next; @@ -106,24 +102,24 @@ static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) unsigned long end = 0; page_t *page = va2page(bgn); - if(0 == page) + if (0 == page) return 0; - end = bgn + cache->objects*cache->size; + end = bgn + cache->objects * cache->size; unsigned long last = bgn; unsigned long addr; - for(addr=bgn; addrsize) + for (addr = bgn; addr < end; addr += cache->size) { - *((void **)last) = (void *) addr; + *((void **)last) = (void *)addr; last = addr; } *((void **)last) = 0; page->freelist = (void **)bgn; - page->inuse = 0; - page->cache = cache; + page->inuse = 0; + page->cache = cache; return page; } @@ -133,13 +129,13 @@ static void *__slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) void **object = 0; page_t *page = 0; - if(cache->page == 0) + if (cache->page == 0) { page = get_partial(cache, gfpflags); - if(page == 0) + if (page == 0) { page = new_slub(cache, gfpflags); - if(page != 0) + if (page != 0) { cache->page = page; } @@ -150,12 +146,12 @@ static void *__slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) } } - if(cache->page == 0) + if (cache->page == 0) return 0; object = cache->page->freelist; - if(object == 0) + if (object == 0) { cache->page = 0; } @@ -172,13 +168,13 @@ static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) { void **object = 0; - if(cache == 0) + if (cache == 0) return 0; unsigned long flags; irq_save(flags); - if(cache->page == 0 || cache->page->freelist == 0) + if (cache->page == 0 || cache->page->freelist == 0) { cache->page = 0; object = __slub_alloc(cache, gfpflags); @@ -203,13 +199,13 @@ static void __slub_free(kmem_cache_t *cache, page_t *page, void *addr) prior = object[0] = page->freelist; page->freelist = object; - if(page->inuse == 0) + if (page->inuse == 0) { list_del(&page->lru); free_pages((unsigned long)page2va(page)); } - if(prior == 0) + if (prior == 0) { list_add(&page->lru, &cache->partial); } @@ -224,7 +220,7 @@ static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) page->inuse--; - if(page == cache->page) + if (page == cache->page) { object[0] = page->freelist; page->freelist = object; @@ -259,10 +255,10 @@ void *kmalloc(size_t size, gfp_t gfpflags) unsigned long flags; irq_save(flags); - for(i=0; iobjsize >= size) + if (p->objsize >= size) { cache = p; break; @@ -293,7 +289,7 @@ kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) { kmem_cache_t *cache = kmalloc(sizeof(kmem_cache_t), 0); - if(cache == 0) + if (cache == 0) return 0; unsigned long flags; @@ -308,16 +304,15 @@ kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) return cache; } - void init_slub_system() { unsigned int i; kmem_cache_t *cache; - for(i=SLUB_MIN_SHIFT; ilist), &slub_caches); //printk("kmem objsize %d\tsize %d \n", cache->objsize, cache->size); -- 2.44.0