From: acevest Date: Tue, 2 Nov 2021 03:14:22 +0000 (+0800) Subject: code style X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=0688583aa092a013ad61ec0412c9b339d851528f;p=kernel.git code style --- diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8f9dc1a --- /dev/null +++ b/.clang-format @@ -0,0 +1,8 @@ +--- +BasedOnStyle: Google +ColumnLimit: 120 +IndentWidth: 4 +TabWidth: 4 + +IndentCaseLabels: false + diff --git a/bin/hello.c b/bin/hello.c index 0fb8a96..aecbfc8 100644 --- a/bin/hello.c +++ b/bin/hello.c @@ -1,19 +1,18 @@ /* *-------------------------------------------------------------------------- * File Name: hello.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 22:44:40 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include #include +#include -int main() -{ +int main() { printf("hello world\n"); return 0; diff --git a/bin/shell.c b/bin/shell.c index 0f2a410..8f68621 100644 --- a/bin/shell.c +++ b/bin/shell.c @@ -1,25 +1,22 @@ /* *-------------------------------------------------------------------------- * File Name: shell.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Wed Feb 24 17:47:22 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include -#include #include +#include void systest(); void sysdebug(unsigned int v); -int main() -{ - - while(1) - { +int main() { + while (1) { systest(); sysdebug(0xAABBCCDD); @@ -28,22 +25,17 @@ int main() read(0, cmd, 256); int len = strlen(cmd); - if(len > 0) - cmd[len-1] = 0; + if (len > 0) cmd[len - 1] = 0; int pid = fork(); - if(pid > 0) - { + if (pid > 0) { wait(pid); - } - else - { + } else { execv(cmd, 0); printf("failed to execute cmd: %s\n", cmd); exit(0); } } - return 0; } diff --git a/boot/boot.c b/boot/boot.c index 86f682b..c3e3ee7 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -1,27 +1,26 @@ /* *-------------------------------------------------------------------------- * File Name: boot.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Wed Dec 30 21:55:29 2009 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include +#include +#include #include #include -#include -#include +#include struct boot_params boot_params __attribute__((aligned(32))); void parse_cmdline(const char *cmdline); -void init_boot_params(multiboot_info_t *p) -{ +void init_boot_params(multiboot_info_t *p) { boot_params.cmdline = (char *)p->cmdline; parse_cmdline(boot_params.cmdline); @@ -37,18 +36,15 @@ void init_boot_params(multiboot_info_t *p) unsigned int i; boot_params.e820map.map_cnt = p->mmap_length / sizeof(memory_map_t); - for (i = 0; i < boot_params.e820map.map_cnt; ++i, ++mmap) - { + for (i = 0; i < boot_params.e820map.map_cnt; ++i, ++mmap) { boot_params.e820map.map[i].addr = mmap->base_addr_low; boot_params.e820map.map[i].size = mmap->length_low; boot_params.e820map.map[i].type = mmap->type; } } -void check_kernel(unsigned long addr, unsigned long magic) -{ - if (magic != MULTIBOOT_BOOTLOADER_MAGIC) - { +void check_kernel(unsigned long addr, unsigned long magic) { + if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { printk("Your boot loader does not support multiboot.\n"); while (1) ; @@ -56,8 +52,7 @@ void check_kernel(unsigned long addr, unsigned long magic) 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) ; diff --git a/boot/cmdline.c b/boot/cmdline.c index de21b2e..4b6fd28 100644 --- a/boot/cmdline.c +++ b/boot/cmdline.c @@ -1,38 +1,33 @@ /* *-------------------------------------------------------------------------- * File Name: cmdline.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Wed Feb 17 17:11:37 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include #include -#include -#include #include +#include +#include -static void get_value(const char *name, char *value) -{ +static void get_value(const char *name, char *value) { const char *p; - if (0 != (p = strstr(system.cmdline, name))) - { - if (0 != (p = strstr(p, "="))) - { + if (0 != (p = strstr(system.cmdline, name))) { + if (0 != (p = strstr(p, "="))) { p++; - while (*p != ' ' && *p != 0) - *value++ = *p++; + while (*p != ' ' && *p != 0) *value++ = *p++; } } *value = 0; } -void parse_cmdline(const char *cmdline) -{ +void parse_cmdline(const char *cmdline) { char value[128]; system.cmdline = cmdline; printk("cmdline: %s\n", system.cmdline); diff --git a/drivers/blk_rw.c b/drivers/blk_rw.c index 1441f59..ce2fc36 100644 --- a/drivers/blk_rw.c +++ b/drivers/blk_rw.c @@ -12,8 +12,7 @@ #include // only support read -void blk_rw(dev_t dev, u64_t offset, u32_t size, char *buf) -{ +void blk_rw(dev_t dev, u64_t offset, u32_t size, char *buf) { assert(DEV_MAJOR(dev) == DEV_MAJOR_HDA); assert(offset % SECT_SIZE == 0); assert(size % SECT_SIZE == 0); diff --git a/drivers/console.c b/drivers/console.c index d8c29e5..1985a4a 100644 --- a/drivers/console.c +++ b/drivers/console.c @@ -7,35 +7,25 @@ * ------------------------------------------------------------------------ */ -#include #include +#include #include cnsl_t cnsl; -static bool empty(const cnsl_queue_t *q) -{ - return q->head == q->tail; -} +static bool empty(const cnsl_queue_t *q) { return q->head == q->tail; } -static bool full(const cnsl_queue_t *q) -{ - return (q->head + 1) % CNSL_QUEUE_SIZE == q->tail; -} +static bool full(const cnsl_queue_t *q) { return (q->head + 1) % CNSL_QUEUE_SIZE == q->tail; } -static void put(cnsl_queue_t *q, char c) -{ - if (!full(q)) - { +static void put(cnsl_queue_t *q, char c) { + if (!full(q)) { q->data[q->head] = c; q->head = (q->head + 1) % CNSL_QUEUE_SIZE; } } -static bool get(cnsl_queue_t *q, char *c) -{ - if (!empty(q)) - { +static bool get(cnsl_queue_t *q, char *c) { + if (!empty(q)) { *c = q->data[q->tail]; q->tail = (q->tail + 1) % CNSL_QUEUE_SIZE; return true; @@ -44,15 +34,10 @@ static bool get(cnsl_queue_t *q, char *c) return false; } -static void clear(cnsl_queue_t *q) -{ - q->head = q->tail = 0; -} +static void clear(cnsl_queue_t *q) { q->head = q->tail = 0; } -static void erase(cnsl_queue_t *q) -{ - if (empty(q)) - return; +static void erase(cnsl_queue_t *q) { + if (empty(q)) return; if (q->head == 0) q->head = CNSL_QUEUE_SIZE - 1; @@ -60,8 +45,7 @@ static void erase(cnsl_queue_t *q) q->head--; } -static void cnsl_queue_init(cnsl_queue_t *cq) -{ +static void cnsl_queue_init(cnsl_queue_t *cq) { memset((void *)cq, 0, sizeof(*cq)); cq->head = 0; cq->tail = 0; @@ -70,71 +54,57 @@ static void cnsl_queue_init(cnsl_queue_t *cq) wait_queue_head_t rdwq; -void cnsl_init() -{ +void cnsl_init() { cnsl_queue_init(&cnsl.rd_q); cnsl_queue_init(&cnsl.wr_q); cnsl_queue_init(&cnsl.sc_q); init_wait_queue(&rdwq); } -int cnsl_kbd_write(char ch) -{ - if (ch == 0) - return 0; +int cnsl_kbd_write(char ch) { + if (ch == 0) return 0; - if (ch == '\b') - { - if (!empty(&cnsl.wr_q)) - vga_putc(0, '\b', 0xF); + if (ch == '\b') { + if (!empty(&cnsl.wr_q)) vga_putc(0, '\b', 0xF); erase(&cnsl.wr_q); erase(&cnsl.sc_q); - } - else - { + } else { put(&cnsl.wr_q, ch); put(&cnsl.sc_q, ch); vga_putc(0, ch, 0xF); } - if (ch == '\n') - { + if (ch == '\n') { clear(&cnsl.wr_q); - while (get(&cnsl.sc_q, &ch)) - put(&cnsl.rd_q, ch); + while (get(&cnsl.sc_q, &ch)) put(&cnsl.rd_q, ch); wake_up(&rdwq); } } -int cnsl_read(char *buf, size_t count) -{ +int cnsl_read(char *buf, size_t count) { unsigned long flags; assert(count > 0); int cnt = 0; - for (cnt = 0; cnt < count;) - { + for (cnt = 0; cnt < count;) { char ch; task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&rdwq, &wait); - while (true) - { + while (true) { task->state = 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') - goto end; + if (ch == '\n') goto end; break; } @@ -148,5 +118,4 @@ end: return cnt; } -chrdev_t cnsl_chrdev = { - .read = cnsl_read}; +chrdev_t cnsl_chrdev = {.read = cnsl_read}; diff --git a/drivers/console.h b/drivers/console.h index 0dc5b04..7e97e87 100644 --- a/drivers/console.h +++ b/drivers/console.h @@ -13,16 +13,14 @@ #define CNSL_QUEUE_SIZE 1024 -typedef struct cnsl_queue -{ +typedef struct cnsl_queue { unsigned int head; unsigned int tail; wait_queue_head_t wait; char data[CNSL_QUEUE_SIZE]; } cnsl_queue_t; -typedef struct cnsl -{ +typedef struct cnsl { cnsl_queue_t rd_q; cnsl_queue_t wr_q; cnsl_queue_t sc_q; diff --git a/drivers/ide.c b/drivers/ide.c index d5169bf..07cf935 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -7,19 +7,18 @@ * ------------------------------------------------------------------------ */ -#include -#include #include -#include #include +#include #include #include +#include #include -#include #include +#include +#include -typedef struct _ide_drv -{ +typedef struct _ide_drv { pci_device_t *pci; unsigned long pio_cnt; unsigned long dma_cnt; @@ -37,16 +36,14 @@ typedef struct _ide_drv part_t part[MAX_SUPPORT_PARTITION_CNT]; } ide_drive_t; -typedef struct prd -{ +typedef struct prd { unsigned int addr; unsigned int cnt : 16; unsigned int reserved : 15; unsigned int eot : 1; } prd_t; -typedef struct -{ +typedef struct { u64_t lba; u32_t scnt; u32_t read_scnt; @@ -73,13 +70,9 @@ unsigned int HD_CHL1_CMD_BASE = 0x170; unsigned int HD_CHL0_CTL_BASE = 0x3F6; 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); -} +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); } -void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) -{ +void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) { drv.pio_cnt++; drv.read_mode = cmd; @@ -88,12 +81,12 @@ void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) outb(0x00, REG_CTL(dev)); outb(0x40 | 0x00, REG_DEVSEL(dev)); - outb((u8)((sect_cnt >> 8) & 0xFF), REG_NSECTOR(dev)); // High + 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_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)); @@ -101,16 +94,14 @@ void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) outb(cmd, REG_CMD(dev)); } -part_t *ide_get_part(dev_t dev) -{ +part_t *ide_get_part(dev_t dev) { assert(DEV_MAJOR(dev) == DEV_MAJOR_HDA); assert(DEV_MINOR(dev) < MAX_SUPPORT_PARTITION_CNT); return drv.part + DEV_MINOR(dev); } -void ide_do_read(u64_t lba, u32_t scnt, char *buf) -{ +void ide_do_read(u64_t lba, u32_t scnt, char *buf) { bool finish = false; unsigned long flags; @@ -131,31 +122,28 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) ide_cmd_out(0, scnt, lba, HD_CMD_READ_EXT); - while (true) - { - //printd("%s pid %d is going to wait\n", __func__, sysc_getpid()); + while (true) { + // printd("%s pid %d is going to wait\n", __func__, sysc_getpid()); task->state = TASK_WAIT; irq_save(flags); finish = r->finish; - //printd("%s pid %d finish %u read_scnt %u scnt %u\n", __func__, sysc_getpid(), r->finish, r->read_scnt, r->scnt); + // 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) - break; + if (finish) break; schedule(); - //printd("%s pid %d is running\n", __func__, sysc_getpid()); + // printd("%s pid %d is running\n", __func__, sysc_getpid()); } - //printd("%s pid %d is really running\n", __func__, sysc_getpid()); + // printd("%s pid %d is really running\n", __func__, sysc_getpid()); task->state = TASK_RUNNING; del_wait_queue(&r->wait, &wait); } unsigned int sys_clock(); -void ide_pci_init(pci_device_t *pci) -{ +void ide_pci_init(pci_device_t *pci) { unsigned int v; v = pci_read_config_word(pci_cmd(pci, PCI_COMMAND)); @@ -174,8 +162,7 @@ void ide_pci_init(pci_device_t *pci) 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); } @@ -188,22 +175,19 @@ void ide_pci_init(pci_device_t *pci) HD_CHL1_CTL_BASE = pci->bars[3] ? pci->bars[3] : HD_CHL1_CTL_BASE; printk("channel0: cmd %04x ctl %04x channel1: cmd %04x ctl %04x\n", HD_CHL0_CMD_BASE, HD_CHL0_CTL_BASE, HD_CHL1_CMD_BASE, HD_CHL1_CTL_BASE); - //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); + // 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() -{ +void ide_status() { u8_t idest = inb(REG_STATUS(0)); u8_t pcist = inb(drv.bus_status); printk(" ide status %02x pci status %02x\n", idest, pcist); } -void ide_debug() -{ +void ide_debug() { unsigned int nsect = 1; char *buf = kmalloc(1 * SECT_SIZE, 0); - if (buf == 0) - panic("out of memory"); + if (buf == 0) panic("out of memory"); ide_do_read(0, nsect, buf); @@ -213,28 +197,24 @@ void ide_debug() kfree(buf); } -void init_pci_controller(unsigned int classcode) -{ +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); + // printl(17, "found pci vendor %04x device %04x class %04x intr %d", pci->vendor, pci->device, pci->classcode, pci->intr_line); ide_pci_init(pci); drv.pci = pci; } } -void ide_default_intr() -{ - //printd("%s\n", __func__); +void ide_default_intr() { + // printd("%s\n", __func__); u8_t status = inb(REG_STATUS(0)); drv.irq_cnt++; status = inb(drv.bus_status); - if (0 == (status & PCI_IDE_STATUS_INTR)) - { + if (0 == (status & PCI_IDE_STATUS_INTR)) { return; } @@ -243,39 +223,32 @@ void ide_default_intr() 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); ide_request.read_scnt++; 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)); } ide_printl(); - //printd(" hard disk sig %04x read mode %x cnt %d\n", sig, drv.read_mode, drv.irq_cnt); + // printd(" hard disk sig %04x read mode %x cnt %d\n", sig, drv.read_mode, drv.irq_cnt); printl(MPL_IDE_INTR, "hard disk sig %x read mode %x cnt %d", sig, drv.read_mode, drv.irq_cnt); outb(PCI_IDE_CMD_STOP, drv.bus_cmd); wake_up(&ide_request.wait); - if (drv.read_mode == HD_CMD_READ_EXT) - { - if (ide_request.read_scnt == ide_request.scnt) - ide_request.finish = true; + if (drv.read_mode == HD_CMD_READ_EXT) { + if (ide_request.read_scnt == ide_request.scnt) ide_request.finish = true; } up(&mutex); } -void ide_irq() -{ - ide_intr_func(); -} +void ide_irq() { ide_intr_func(); } prd_t prd __attribute__((aligned(64 * 1024))); unsigned long gprdt = 0; @@ -288,8 +261,7 @@ unsigned long gprdt = 0; inb(HD_CHL0_CTL_BASE); \ } -void ide_dma_pci_lba48() -{ +void ide_dma_pci_lba48() { drv.dma_cnt++; drv.read_mode = HD_CMD_READ_DMA; #if 1 @@ -302,8 +274,7 @@ void ide_dma_pci_lba48() prd.eot = 1; gprdt = va2pa(&prd); - printl(16, "gprdt %08x &prdt %08x prd.addr %08x addr %08x", - gprdt, &prd, prd.addr, addr); + printl(16, "gprdt %08x &prdt %08x prd.addr %08x addr %08x", gprdt, &prd, prd.addr, addr); outb(PCI_IDE_CMD_STOP, drv.bus_cmd); unsigned short status = inb(drv.bus_status); @@ -337,7 +308,7 @@ 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); @@ -363,55 +334,46 @@ void ide_dma_pci_lba48() 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 + u16_t scnth; // sector count high + u32_t lba; // lba low + u32_t scnt; // sector count } hd_part_t; -void ide_read_extended_partition(u64_t lba, unsigned int inx) -{ - if (inx >= MAX_SUPPORT_PARTITION_CNT) - return; +void ide_read_extended_partition(u64_t lba, unsigned int inx) { + if (inx >= MAX_SUPPORT_PARTITION_CNT) return; unsigned int i; char *buf = kmalloc(512, 0); - if (buf == 0) - panic("no memory"); + if (buf == 0) panic("no memory"); ide_do_read(lba, 1, buf); u16_t sig = *((u16_t *)(buf + 510)); - if (sig != 0xAA55) - panic("bad partition sect"); + if (sig != 0xAA55) panic("bad partition sect"); hd_part_t *p = (hd_part_t *)(buf + PARTITION_TABLE_OFFSET); printd("%s:%d lba %d \n", __func__, __LINE__, lba); - for (i = 0; i < PARTITION_CNT; ++i, ++p) - { - if (p->type == 0) - continue; + for (i = 0; i < PARTITION_CNT; ++i, ++p) { + 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|((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; - if (p->type != 0x05) - { + if (p->type != 0x05) { 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 - { + 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); @@ -421,41 +383,34 @@ void ide_read_extended_partition(u64_t lba, unsigned int inx) kfree(buf); } -void ide_read_partition() -{ +void ide_read_partition() { printk("reading partitions....\n"); unsigned int i; char *buf = kmalloc(512, 0); - if (buf == 0) - panic("no memory"); + if (buf == 0) panic("no memory"); ide_do_read(0, 1, buf); u16_t sig = *((u16_t *)(buf + 510)); - if (sig != 0xAA55) - panic("bad partition sect"); + if (sig != 0xAA55) panic("bad partition sect"); hd_part_t *p = (hd_part_t *)(buf + PARTITION_TABLE_OFFSET); unsigned int ext_inx = ~0U; - for (i = 0; i < PARTITION_CNT; ++i, ++p) - { - if (p->type == 0) - continue; + for (i = 0; i < PARTITION_CNT; ++i, ++p) { + 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|((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; - if (p->type == 0x05) - { - if (drv.ext_lba_base == 0) - { + if (p->type == 0x05) { + if (drv.ext_lba_base == 0) { drv.ext_lba_base = drv.part[i].lba_start; ext_inx = i; } @@ -466,12 +421,10 @@ void ide_read_partition() kfree(buf); - if (ext_inx != ~0U) - ide_read_extended_partition(drv.part[ext_inx].lba_start, 4); + if (ext_inx != ~0U) ide_read_extended_partition(drv.part[ext_inx].lba_start, 4); } -void ide_init() -{ +void ide_init() { memset((void *)&drv, 0, sizeof(drv)); init_pci_controller(0x0106); diff --git a/drivers/ide.h b/drivers/ide.h index 87a6b4b..e6d782c 100644 --- a/drivers/ide.h +++ b/drivers/ide.h @@ -118,8 +118,7 @@ extern unsigned int HD_CHL1_CTL_BASE; #define PARTITION_TABLE_OFFSET 0x1BE #define MAX_SUPPORT_PARTITION_CNT 16 -typedef struct -{ +typedef struct { u64_t lba_start; u64_t lba_end; } part_t; diff --git a/drivers/keyboard.c b/drivers/keyboard.c index 3af4e81..82830f3 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -1,24 +1,24 @@ /* *-------------------------------------------------------------------------- * File Name: keyboard.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Thu Jul 16 18:39:57 2009 * Last Update: Thu Jul 16 18:39:57 2009 - * + * *-------------------------------------------------------------------------- */ +#include +#include #include -#include -#include #include -#include -#include +#include +#include void reboot(); void poweroff(); @@ -32,383 +32,28 @@ 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, + 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; scan_code = inb(0x60); kbd_debug(scan_code); - if (0x80 & scan_code) // break code + if (0x80 & scan_code) // break code { return; } @@ -418,46 +63,45 @@ void kbd_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) cnsl_kbd_write(ch); } -void kbd_debug(unsigned char scan_code) -{ +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) ; } - if (scan_code == 0x58) // F12 + if (scan_code == 0x58) // F12 vga_dbg_toggle(); - //ide_status(); + // ide_status(); } diff --git a/drivers/vga.c b/drivers/vga.c index e14369c..fb4ae2c 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -1,20 +1,19 @@ /* *-------------------------------------------------------------------------- * File Name: vga.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jul 18 23:01:18 2009 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include #include -typedef struct -{ +typedef struct { u8_t c; u8_t f; } __attribute__((packed)) vga_char_t; @@ -36,8 +35,7 @@ typedef struct #define TAB_ALIGN 4 #define TAB_MASK (TAB_ALIGN - 1) -typedef struct -{ +typedef struct { unsigned int id; vga_char_t *base; unsigned int offset; @@ -45,35 +43,20 @@ typedef struct #define VGA_SCREEN_CNT 3 #define VGA_MAX_SCREEN_CNT ((VGA_SCREEN_CNT) + 1) -unsigned int vga_screen_cnt() -{ - return VGA_SCREEN_CNT; -} +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; -static void set_offset(vga_screen_t *s, unsigned int x, unsigned int y) -{ - s->offset = (y * CHARS_PER_LINE) + x; -} +static void set_offset(vga_screen_t *s, unsigned int x, unsigned int y) { s->offset = (y * CHARS_PER_LINE) + x; } -static unsigned int xpos(vga_screen_t *s) -{ - return (s->offset % CHARS_PER_LINE); -} +static unsigned int xpos(vga_screen_t *s) { return (s->offset % CHARS_PER_LINE); } -static unsigned int ypos(vga_screen_t *s) -{ - return (s->offset / CHARS_PER_LINE); -} +static unsigned int ypos(vga_screen_t *s) { return (s->offset / CHARS_PER_LINE); } -vga_char_t vga_char(unsigned char c, unsigned char f) -{ +vga_char_t vga_char(unsigned char c, unsigned char f) { vga_char_t x; x.c = c; x.f = f; @@ -81,8 +64,7 @@ vga_char_t vga_char(unsigned char c, unsigned char f) return x; } -void vga_set_cursor_pos(vga_screen_t *s) -{ +void vga_set_cursor_pos(vga_screen_t *s) { 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; @@ -97,10 +79,8 @@ void vga_set_cursor_pos(vga_screen_t *s) irq_restore(flags); } -void vga_clear(vga_screen_t *s, unsigned int b, unsigned int e) -{ - if (e <= b) - return; +void vga_clear(vga_screen_t *s, unsigned int b, unsigned int e) { + if (e <= b) return; vga_char_t *base = s->base; @@ -109,27 +89,24 @@ void vga_clear(vga_screen_t *s, unsigned int b, unsigned int e) memset((void *)base, 0, (e - b) * sizeof(vga_char_t)); } -void vga_scroll_up(vga_screen_t *s) -{ +void vga_scroll_up(vga_screen_t *s) { int delta = ypos(s) + 1 - LINES_PER_SCREEN; - if (delta <= 0) - return; + 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; memcpy((void *)base, (void *)head, (empt - base) * sizeof(vga_char_t)); - //memset((void *)empt, 0, delta*BYTES_PER_LINE); + // memset((void *)empt, 0, delta*BYTES_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) -{ +void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) { vga_screen_t *s = vga_screen + nr; vga_char_t *pv = s->base; @@ -138,8 +115,7 @@ void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) bool need_forward = true; unsigned int old_offset = s->offset; - switch (c) - { + switch (c) { case '\r': set_offset(s, 0, ypos(s)); break; @@ -161,44 +137,36 @@ void vga_putc(unsigned int nr, unsigned char c, const unsigned char color) break; } - if (need_clear) - { + if (need_clear) { vga_clear(s, old_offset, s->offset); } vga_scroll_up(s); - if (vga_current_screen == s) - vga_set_cursor_pos(s); + if (vga_current_screen == s) vga_set_cursor_pos(s); } -void vga_puts(unsigned int nr, const char *buf, unsigned char color) -{ +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; - while (*p) - { + while (*p) { vga_putc(nr, *p, color); p++; } } -void __vga_switch(unsigned int offset) -{ +void __vga_switch(unsigned int offset) { outb(VGA_CRTC_START_ADDR_H, VGA_CRTC_ADDR); outb((offset >> 8) & 0xFF, VGA_CRTC_DATA); outb(VGA_CRTC_START_ADDR_L, VGA_CRTC_ADDR); outb((offset)&0xFF, VGA_CRTC_DATA); } -void vga_switch(unsigned int nr) -{ - if (nr >= VGA_MAX_SCREEN_CNT) - return; +void vga_switch(unsigned int nr) { + if (nr >= VGA_MAX_SCREEN_CNT) return; vga_screen_t *s = vga_screen + nr; @@ -211,12 +179,10 @@ void vga_switch(unsigned int nr) #define VIDEO_DBG_LINE ((VGA_MAX_SCREEN_CNT) * (MAX_LINES_PER_SCREEN)) -void vga_dbg_toggle() -{ +void vga_dbg_toggle() { static bool dbg = true; unsigned int offset = 0; - if (dbg) - { + if (dbg) { offset += VIDEO_DBG_LINE * CHARS_PER_LINE; } @@ -227,20 +193,16 @@ void vga_dbg_toggle() unsigned char vga_dbg_color = 0x1F; -void vga_dbg_clear() -{ +void vga_dbg_clear() { int i; int line; - for (line = 0; line < MAX_LINES_PER_SCREEN; ++line) - { + for (line = 0; line < MAX_LINES_PER_SCREEN; ++line) { vga_char_t *const pv = (vga_char_t *const)(VIDEO_ADDR + (VIDEO_DBG_LINE + line) * BYTES_PER_LINE); - for (i = 0; i < CHARS_PER_LINE; ++i) - pv[i] = vga_char(0, vga_dbg_color); + for (i = 0; i < CHARS_PER_LINE; ++i) pv[i] = vga_char(0, vga_dbg_color); } } -void vga_dbg_puts(unsigned int line, unsigned int offset, const char *buf) -{ +void vga_dbg_puts(unsigned int line, unsigned int offset, const char *buf) { assert(line < LINES_PER_SCREEN); assert(offset < CHARS_PER_LINE); @@ -248,17 +210,14 @@ void vga_dbg_puts(unsigned int line, unsigned int offset, const char *buf) char *p = (char *)buf; vga_char_t *const pv = (vga_char_t *const)(VIDEO_ADDR + (VIDEO_DBG_LINE + line) * BYTES_PER_LINE + offset * sizeof(vga_char_t)); - for (i = 0; *p; ++i, ++p) - { + for (i = 0; *p; ++i, ++p) { pv[i] = vga_char(*p, vga_dbg_color); } } -void vga_init() -{ +void vga_init() { unsigned int i; - for (i = 1; i < VGA_MAX_SCREEN_CNT; ++i) - { + for (i = 1; i < VGA_MAX_SCREEN_CNT; ++i) { memset(vga_screen + i, 0, sizeof(vga_screen_t)); vga_screen[i].id = i; vga_screen[i].base = (vga_char_t *)(VIDEO_ADDR + i * MAX_LINES_PER_SCREEN * BYTES_PER_LINE); diff --git a/fs/ext2.c b/fs/ext2.c index f8aa9c8..5182191 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -6,13 +6,13 @@ * Description: none * ------------------------------------------------------------------------ */ -#include "system.h" +#include "ext2.h" + #include "fs.h" #include "mm.h" -#include "ext2.h" +#include "system.h" -struct -{ +struct { ext2_sb_t ext2_sb; ext2_gd_t *ext2_gd; } ext2_fs; @@ -20,8 +20,7 @@ struct extern void blk_rw(dev_t dev, u64_t offset, u32_t scnt, char *buf); #define BLKRW(blkid, blkcnt, buf) \ - do \ - { \ + do { \ blk_rw(system.root_dev, 1ULL * (blkid)*EXT2_BLOCK_SIZE, (blkcnt)*EXT2_BLOCK_SIZE, buf); \ } while (0) @@ -33,43 +32,30 @@ ext2_inode_t ext2_root_inode; static ext2_inode_t boot_inode; static ext2_inode_t krnl_inode; -unsigned long ext2_block_size() -{ - return (EXT2_MIN_BLOCK_SIZE << (EXT2_SB)->s_log_block_size); -} +unsigned long ext2_block_size() { return (EXT2_MIN_BLOCK_SIZE << (EXT2_SB)->s_log_block_size); } -void *ext2_alloc_block() -{ - return (void *)kmem_cache_alloc(ext2_block_cache, 0); -} +void *ext2_alloc_block() { return (void *)kmem_cache_alloc(ext2_block_cache, 0); } -void *ext2_free_block(void *blk) -{ - kmem_cache_free(ext2_block_cache, blk); -} +void *ext2_free_block(void *blk) { kmem_cache_free(ext2_block_cache, blk); } -void *ext2_alloc_inode() -{ - return (void *)kmem_cache_alloc(ext2_inode_cache, 0); -} +void *ext2_alloc_inode() { return (void *)kmem_cache_alloc(ext2_inode_cache, 0); } #define ext2_gd(n) ((ext2_gd_t *)(EXT2_GD) + (n)) -void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) -{ +void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) { void *blk = ext2_alloc_block(); assert(blk != 0); printd("read_inode %u\n", ino); - unsigned int in; // inode number - unsigned int gn; // group number - unsigned int gi; // inode index in group + unsigned int in; // inode number + unsigned int gn; // group number + unsigned int gi; // inode index in group gn = (ino - 1) / EXT2_INODES_PER_GROUP; gi = (ino - 1) % EXT2_INODES_PER_GROUP; - unsigned int blkid = gi / EXT2_INODES_PER_BLOCK; // inode blkid - unsigned int inoff = gi % EXT2_INODES_PER_BLOCK; // inode offset + unsigned int blkid = gi / EXT2_INODES_PER_BLOCK; // inode blkid + unsigned int inoff = gi % EXT2_INODES_PER_BLOCK; // inode offset blkid += ext2_gd(gn)->bg_inode_table; inoff *= EXT2_INODE_SIZE; @@ -85,23 +71,20 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) ext2_free_block(blk); } -void ext2_read_file(const ext2_inode_t *inode, char *buf) -{ +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); unsigned int blkcnt = inode->i_size / EXT2_BLOCK_SIZE; int i = 0; - for (i = 0; i < blkcnt; ++i) - { + for (i = 0; i < blkcnt; ++i) { 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(); @@ -113,13 +96,12 @@ void ext2_read_file(const ext2_inode_t *inode, char *buf) printd("read file done\n"); } -void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, char *buf) -{ +void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, char *buf) { 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 + 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); @@ -129,27 +111,23 @@ void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, 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 ext2_search_indir(const char *name, const ext2_inode_t *inode, unsigned int *file_type) { unsigned int ino = 0; *file_type = EXT2_FT_UNKNOWN; void *blk = ext2_alloc_block(); assert(blk != 0); - BLKRW(inode->i_block[0], 1, blk); // only support the first direct blocks + BLKRW(inode->i_block[0], 1, blk); // only support the first direct blocks 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); + 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); - if (strcmp(name, tmp) == 0) - { + if (strcmp(name, tmp) == 0) { ino = dirent->inode; *file_type = dirent->file_type; break; @@ -157,8 +135,7 @@ unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsi 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; } @@ -169,25 +146,20 @@ unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsi return ino; } -static int get_filename_from_path(const char *path, char *file) -{ +static int get_filename_from_path(const char *path, char *file) { int i = 0; - while (*path == '/' && *path != '\0') - path++; + while (*path == '/' && *path != '\0') path++; - while (*path != '/' && *path != '\0') - file[i++] = *path++; + while (*path != '/' && *path != '\0') file[i++] = *path++; file[i] = 0; return i; } -unsigned int ext2_search_inpath(const char *path) -{ - if (path == 0 || strlen(path) == 0 || path[0] != '/') - return 0; +unsigned int ext2_search_inpath(const char *path) { + if (path == 0 || strlen(path) == 0 || path[0] != '/') return 0; assert(path != 0); assert(strlen(path) > 0); @@ -204,65 +176,53 @@ unsigned int ext2_search_inpath(const char *path) 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) - return 0; - //assert(ino != 0); + if (ino == 0) return 0; + // assert(ino != 0); path += len; - if (*path != 0) - { + if (*path != 0) { path++; ext2_read_inode(ino, inode); - } - else - { + } else { return 0; assert(0); } } - if (file_type != EXT2_FT_REG_FILE) - return 0; + if (file_type != EXT2_FT_REG_FILE) return 0; return ino; } -void ext2_setup_fs() -{ +void ext2_setup_fs() { memset(&ext2_fs, 0, sizeof(ext2_fs)); char *buf = kmalloc(EXT2_BLOCK_SIZE, 0); - if (buf == 0) - panic("out of memory"); + 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..."); } 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); - 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); + 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); + 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); 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) - panic("setup ext2 block cache failed. out of memory"); + 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) - panic("setup ext2 inode cache failed. out of memory"); + 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); @@ -272,10 +232,9 @@ void ext2_setup_fs() 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; i < gps; ++i) - { - printd(" [%2u] inode table %u free blocks %u free inode %u used dir %u\n", - 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); + for (i = 0; i < gps; ++i) { + printd(" [%2u] inode table %u free blocks %u free inode %u used dir %u\n", 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 78999cd..0088189 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: fs.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Fri Feb 12 20:48:50 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include @@ -16,18 +16,11 @@ extern chrdev_t cnsl_chrdev; -chrdev_t *chrdev[CHRDEV_SIZE] = { - &cnsl_chrdev}; +chrdev_t *chrdev[CHRDEV_SIZE] = {&cnsl_chrdev}; void ext2_setup_fs(); unsigned int ext2_search_inpath(const char *path); -void setup_fs() -{ - ext2_setup_fs(); -} +void setup_fs() { ext2_setup_fs(); } -unsigned int namei(const char *path) -{ - return ext2_search_inpath(path); -} +unsigned int namei(const char *path) { return ext2_search_inpath(path); } diff --git a/fs/open.c b/fs/open.c index a7dfbe9..46ff07e 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1,24 +1,21 @@ /* *-------------------------------------------------------------------------- * File Name: open.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Feb 20 18:53:47 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include +#include #include -#include -#include #include -#include +#include +#include #include +#include -int sysc_open(const char *path, int flags, mode_t mode) -{ - return 0; -} +int sysc_open(const char *path, int flags, mode_t mode) { return 0; } diff --git a/fs/read.c b/fs/read.c index 7a3d10f..3c24678 100644 --- a/fs/read.c +++ b/fs/read.c @@ -1,24 +1,22 @@ /* *-------------------------------------------------------------------------- * File Name: read.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 18:53:47 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include #include -#include #include +#include #include +#include -int sysc_read(int fd, void *buf, size_t count) -{ - if (fd < 0 || fd >= NR_OPENS) - return -EBADF; +int sysc_read(int fd, void *buf, size_t count) { + if (fd < 0 || fd >= NR_OPENS) return -EBADF; // only support char device // only support read from console. diff --git a/fs/stat.c b/fs/stat.c index 64ef9df..482b3e1 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -1,21 +1,20 @@ /* *-------------------------------------------------------------------------- * File Name: stat.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 19:56:08 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include -#include #include #include #include -int sysc_stat(int fd, struct stat *stat) -{ +#include +#include +int sysc_stat(int fd, struct stat *stat) { #if 0 if(fd<0 || fd>=NR_OPENS) return -EBADF; diff --git a/fs/write.c b/fs/write.c index 729ce9d..04f5b51 100644 --- a/fs/write.c +++ b/fs/write.c @@ -1,29 +1,26 @@ /* *-------------------------------------------------------------------------- * File Name: write.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Mar 8 11:05:12 2009 * Last Update: Sun Mar 8 11:05:12 2009 - * + * *-------------------------------------------------------------------------- */ #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; +int sysc_write(int fd, const char *buf, unsigned long size) { + if (size < 0) return -1; - switch (fd) - { + switch (fd) { case 0: vga_puts(0, buf, 0xF); break; diff --git a/include/assert.h b/include/assert.h index fd02b99..2e67f27 100644 --- a/include/assert.h +++ b/include/assert.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: assert.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jan 23 14:02:00 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -15,9 +15,6 @@ #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 +#endif //_ASSERT_H diff --git a/include/bak.ext2.h b/include/bak.ext2.h index 2dccc34..0bbe30f 100644 --- a/include/bak.ext2.h +++ b/include/bak.ext2.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: ext2.h - * + * * Description: 当然.几乎来自Linux 内核. - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Dec 26 22:43:43 2008 * Last Update: Fri Dec 26 22:43:43 2008 - * + * *-------------------------------------------------------------------------- */ @@ -43,7 +43,7 @@ #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 @@ -60,8 +60,7 @@ * EXT2 FILE SYSTEM PART * ------------------------------------------------------------------------ */ -typedef struct ext2_superblock -{ +typedef struct ext2_superblock { /* u32 s_inodes_count; u32 s_blocks_count; @@ -110,7 +109,7 @@ typedef struct ext2_superblock * the incompatible feature set is that if there is a bit set * in the incompatible feature set that the kernel doesn't * know about, it should refuse to mount the filesystem. - * + * * e2fsck's requirements are more strict; if it doesn't know * about a feature in either the compatible or incompatible * feature set, it must abort and not try to meddle with @@ -151,8 +150,7 @@ typedef struct ext2_superblock extern SuperBlock ext2_sb; -typedef struct ext2_group_descriptor -{ +typedef struct ext2_group_descriptor { u32 bg_block_bitmap; u32 bg_inode_bitmap; u32 bg_inode_table; @@ -169,8 +167,7 @@ typedef struct ext2_group_descriptor #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) -typedef struct ext2_inode -{ +typedef struct ext2_inode { u16 i_mode; u16 i_uid; u32 i_size; @@ -192,8 +189,7 @@ typedef struct ext2_inode } Inode, *pInode; #define EXT2_NAME_LEN 255 -typedef struct ext2_dir_ent -{ +typedef struct ext2_dir_ent { u32 inode; u16 rec_len; u8 name_len; @@ -205,8 +201,7 @@ typedef struct ext2_dir_ent * Ext2 目录类型. * 到目前为止只有低3位有效. */ -enum -{ +enum { EXT2_FT_UNKNOWN, EXT2_FT_REG_FILE, EXT2_FT_DIR, @@ -220,12 +215,11 @@ enum #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_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); -#endif //_EXT2_H +#endif //_EXT2_H diff --git a/include/bits.h b/include/bits.h index 7f49882..50f37fd 100644 --- a/include/bits.h +++ b/include/bits.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: bits.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Mar 4 20:56:29 2009 * Last Update: Wed Mar 4 20:56:29 2009 - * + * *-------------------------------------------------------------------------- */ @@ -28,15 +28,9 @@ #define BITS_PER_LONG (sizeof(unsigned long) * 8) -static inline void btc(unsigned int *v, unsigned int b) -{ - asm("btc %1,%0" - : "=m"(*v) - : "Ir"(b)); -} +static inline void btc(unsigned int *v, unsigned int b) { asm("btc %1,%0" : "=m"(*v) : "Ir"(b)); } -static inline int test_and_set_bit(long nr, volatile unsigned long *addr) -{ +static inline int test_and_set_bit(long nr, volatile unsigned long *addr) { int oldbit; asm("bts %2,%1\n\t" @@ -46,15 +40,15 @@ static inline int test_and_set_bit(long nr, volatile unsigned long *addr) return oldbit; } -static inline int test_and_clear_bit(int nr, volatile unsigned long *addr) -{ +static inline int test_and_clear_bit(int nr, volatile unsigned long *addr) { int oldbit; - asm volatile("btr %2,%1\n\t" - "sbb %0,%0" - : "=r"(oldbit), "+m"(*(volatile long *)(addr)) - : "Ir"(nr) - : "memory"); + asm volatile( + "btr %2,%1\n\t" + "sbb %0,%0" + : "=r"(oldbit), "+m"(*(volatile long *)(addr)) + : "Ir"(nr) + : "memory"); return oldbit; } @@ -66,15 +60,15 @@ static inline int test_and_clear_bit(int nr, volatile unsigned long *addr) * This operation is atomic and cannot be reordered. * It also implies a memory barrier. */ -static inline int test_and_change_bit(int nr, volatile unsigned long *addr) -{ +static inline int test_and_change_bit(int nr, volatile unsigned long *addr) { int oldbit; - asm volatile("btc %2,%1\n\t" - "sbb %0,%0" - : "=r"(oldbit), "+m"(*(volatile long *)(addr)) - : "Ir"(nr) - : "memory"); + asm volatile( + "btc %2,%1\n\t" + "sbb %0,%0" + : "=r"(oldbit), "+m"(*(volatile long *)(addr)) + : "Ir"(nr) + : "memory"); return oldbit; } @@ -88,17 +82,12 @@ static inline int test_and_change_bit(int nr, volatile unsigned long *addr) * If it's called on the same region of memory simultaneously, the effect * may be that only one operation succeeds. */ -static inline void change_bit(int nr, volatile unsigned long *addr) -{ - asm volatile("btc %1,%0" - : "+m"(*(volatile long *)(addr)) - : "Ir"(nr)); +static inline void change_bit(int nr, volatile unsigned long *addr) { + asm volatile("btc %1,%0" : "+m"(*(volatile long *)(addr)) : "Ir"(nr)); } -static inline int constant_test_bit(unsigned int nr, const volatile unsigned long *addr) -{ - return ((1UL << (nr % BITS_PER_LONG)) & - (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; +static inline int constant_test_bit(unsigned int nr, const volatile unsigned long *addr) { + return ((1UL << (nr % BITS_PER_LONG)) & (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; } /** @@ -109,8 +98,7 @@ static inline int constant_test_bit(unsigned int nr, const volatile unsigned lon * Returns the bit-number of the first set bit, not the number of the byte * containing a bit. */ -static inline int find_first_bit(const unsigned long *addr, unsigned size) -{ +static inline int find_first_bit(const unsigned long *addr, unsigned size) { int d0, d1; int res; @@ -138,13 +126,11 @@ static inline int find_first_bit(const unsigned long *addr, unsigned size) * Returns the bit-number of the first zero bit, not the number of the byte * containing a bit. */ -static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) -{ +static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) { int d0, d1, d2; int res; - if (!size) - return 0; + if (!size) return 0; /* This looks at memory. Mark it volatile to tell gcc not to move it around */ __asm__ __volatile__( "movl $-1,%%eax\n\t" @@ -169,24 +155,22 @@ static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) * @offset: The bitnumber to start searching at * @size: The maximum size to search */ -static inline int find_next_zero_bit(const unsigned long *addr, int size, int offset) -{ +static inline int find_next_zero_bit(const unsigned long *addr, int size, int offset) { 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))); - if (set < (32 - bit)) - return set + offset; + __asm__( + "bsfl %1,%0\n\t" + "jne 1f\n\t" + "movl $32, %0\n" + "1:" + : "=r"(set) + : "r"(~(*p >> bit))); + if (set < (32 - bit)) return set + offset; set = 32 - bit; p++; } @@ -197,15 +181,15 @@ static inline int find_next_zero_bit(const unsigned long *addr, int size, int of return (offset + set + res); } -static inline int variable_test_bit(int nr, volatile const unsigned long *addr) -{ +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)); + asm volatile( + "bt %2,%1\n\t" + "sbb %0,%0" + : "=r"(oldbit) + : "m"(*(unsigned long *)addr), "Ir"(nr)); return oldbit; } -#endif //_BITS_H +#endif //_BITS_H diff --git a/include/boot/boot.h b/include/boot/boot.h index 3507d5e..840cf18 100644 --- a/include/boot/boot.h +++ b/include/boot/boot.h @@ -23,38 +23,34 @@ #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_lower; // in bytes unsigned long mem_upper; struct e820map e820map; }; -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_hit_pfn; // last hit index in bitmap + unsigned long last_offset; // offset to pfn2pa(this->min_pfn); + unsigned long last_hit_pfn; // last hit index in bitmap void *bitmap; unsigned long mapsize; diff --git a/include/boot/multiboot.h b/include/boot/multiboot.h index e333adc..e81eecb 100644 --- a/include/boot/multiboot.h +++ b/include/boot/multiboot.h @@ -1,16 +1,16 @@ /* multiboot.h - the header for Multiboot */ /* Copyright (C) 1999, 2001 Free Software Foundation, Inc. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ @@ -19,26 +19,26 @@ #pragma once /* The magic number for the Multiboot header. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 +#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 /* The flags for the Multiboot header. */ #ifdef __ELF__ -# define MULTIBOOT_HEADER_FLAGS 0x00000003 +#define MULTIBOOT_HEADER_FLAGS 0x00000003 #else -# define MULTIBOOT_HEADER_FLAGS 0x00010003 +#define MULTIBOOT_HEADER_FLAGS 0x00010003 #endif /* The magic number passed by a Multiboot-compliant boot loader. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 +#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 /* The size of our stack (16KB). */ -#define MULTIBOOT_STACK_SIZE 0x4000 +#define MULTIBOOT_STACK_SIZE 0x4000 /* C symbol format. HAVE_ASM_USCORE is defined by configure. */ #ifdef HAVE_ASM_USCORE -# define EXT_C(sym) _ ## sym +#define EXT_C(sym) _##sym #else -# define EXT_C(sym) sym +#define EXT_C(sym) sym #endif #ifndef ASM @@ -47,75 +47,67 @@ /* Types. */ /* The Multiboot header. */ -typedef struct multiboot_header -{ - unsigned long magic; - unsigned long flags; - unsigned long checksum; - unsigned long header_addr; - unsigned long load_addr; - unsigned long load_end_addr; - unsigned long bss_end_addr; - unsigned long entry_addr; +typedef struct multiboot_header { + unsigned long magic; + unsigned long flags; + unsigned long checksum; + unsigned long header_addr; + unsigned long load_addr; + unsigned long load_end_addr; + unsigned long bss_end_addr; + unsigned long entry_addr; } multiboot_header_t; /* The symbol table for a.out. */ -typedef struct aout_symbol_table -{ - unsigned long tabsize; - unsigned long strsize; - unsigned long addr; - unsigned long reserved; +typedef struct aout_symbol_table { + unsigned long tabsize; + unsigned long strsize; + unsigned long addr; + unsigned long reserved; } aout_symbol_table_t; /* The section header table for ELF. */ -typedef struct elf_section_header_table -{ - unsigned long num; - unsigned long size; - unsigned long addr; - unsigned long shndx; +typedef struct elf_section_header_table { + unsigned long num; + unsigned long size; + unsigned long addr; + unsigned long shndx; } elf_section_header_table_t; /* The Multiboot information. */ -typedef struct multiboot_info -{ - unsigned long flags; - unsigned long mem_lower; - unsigned long mem_upper; - unsigned long boot_device; - unsigned long cmdline; - unsigned long mods_count; - unsigned long mods_addr; - union - { - aout_symbol_table_t aout_sym; - elf_section_header_table_t elf_sec; - } u; - unsigned long mmap_length; - unsigned long mmap_addr; +typedef struct multiboot_info { + unsigned long flags; + unsigned long mem_lower; + unsigned long mem_upper; + unsigned long boot_device; + unsigned long cmdline; + unsigned long mods_count; + unsigned long mods_addr; + union { + aout_symbol_table_t aout_sym; + elf_section_header_table_t elf_sec; + } u; + unsigned long mmap_length; + unsigned long mmap_addr; } multiboot_info_t; - /* The module structure. */ -typedef struct module -{ - unsigned long mod_start; - unsigned long mod_end; - unsigned long string; - unsigned long reserved; +typedef struct module { + unsigned long mod_start; + unsigned long mod_end; + unsigned long string; + unsigned long reserved; } module_t; /* The memory map. Be careful that the offset 0 is base_addr_low but no size. */ -typedef struct memory_map -{ - unsigned long size; - unsigned long base_addr_low; - unsigned long base_addr_high; - unsigned long length_low; - unsigned long length_high; - unsigned long type; +typedef struct memory_map { + unsigned long size; + unsigned long base_addr_low; + unsigned long base_addr_high; + unsigned long length_low; + unsigned long length_high; + unsigned long type; } memory_map_t; typedef multiboot_info_t MultiBootInfo, *pMultiBootInfo; diff --git a/include/bug.h b/include/bug.h index 6548715..e18af1d 100644 --- a/include/bug.h +++ b/include/bug.h @@ -10,15 +10,12 @@ #pragma once #define BUG() \ - do \ - { \ + do { \ printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \ panic("BUG!"); \ } while (0) -#define BUG_ON(condition) \ - do \ - { \ - if (unlikely((condition) != 0)) \ - BUG(); \ +#define BUG_ON(condition) \ + do { \ + if (unlikely((condition) != 0)) BUG(); \ } while (0) diff --git a/include/elf.h b/include/elf.h index 1b1e99d..50b8afa 100644 --- a/include/elf.h +++ b/include/elf.h @@ -1,18 +1,20 @@ /* *-------------------------------------------------------------------------- * File Name: elf.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jan 30 19:36:15 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #ifndef _ELF_H #define _ELF_H +#include + typedef u16 Elf32_Half; typedef u32 Elf32_Word; typedef s32 Elf32_Sword; @@ -31,8 +33,7 @@ typedef Elf32_Half Elf32_Versym; */ #define EI_NIDENT (16) -typedef struct -{ +typedef struct { unsigned char e_ident[EI_NIDENT]; Elf32_Half e_type; Elf32_Half e_machine; @@ -41,12 +42,12 @@ typedef struct 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_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_Half e_shstrndx; // Section Header String Table Index } Elf32_Ehdr, *pElf32_Ehdr; #define ELFMAG "\177ELF" @@ -127,8 +128,7 @@ typedef struct * 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 */ @@ -147,8 +147,7 @@ typedef struct *-------------------------------------------------------------------------- */ -typedef struct -{ +typedef struct { Elf32_Word p_type; Elf32_Off p_offset; Elf32_Addr p_vaddr; @@ -181,4 +180,4 @@ typedef struct #define PT_LOPROC 0x70000000 /* Start of processor-specific */ #define PT_HIPROC 0x7FFFFFFF /* End of processor-specific */ -#endif //_ELF_H +#endif //_ELF_H diff --git a/include/errno.h b/include/errno.h index 1c0778c..8e48609 100644 --- a/include/errno.h +++ b/include/errno.h @@ -1,10 +1,10 @@ /* *-------------------------------------------------------------------------- * File Name: errno.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Mon Feb 1 17:07:06 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -48,4 +48,4 @@ extern int errno; -#endif //_ERRNO_H +#endif //_ERRNO_H diff --git a/include/ext2.h b/include/ext2.h index 7a8b29b..ef26ee0 100644 --- a/include/ext2.h +++ b/include/ext2.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: ext2.h - * + * * Description: 当然.几乎来自Linux 内核. - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Dec 26 22:43:43 2008 * Last Update: Fri Dec 26 22:43:43 2008 - * + * *-------------------------------------------------------------------------- */ @@ -45,7 +45,7 @@ unsigned long ext2_block_size(); #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 @@ -62,8 +62,7 @@ unsigned long ext2_block_size(); * EXT2 FILE SYSTEM PART * ------------------------------------------------------------------------ */ -typedef struct ext2_superblock -{ +typedef struct ext2_superblock { u32 s_inodes_count; /* Inodes count */ u32 s_blocks_count; /* Blocks count */ u32 s_r_blocks_count; /* Reserved blocks count */ @@ -96,7 +95,7 @@ typedef struct ext2_superblock * the incompatible feature set is that if there is a bit set * in the incompatible feature set that the kernel doesn't * know about, it should refuse to mount the filesystem. - * + * * e2fsck's requirements are more strict; if it doesn't know * about a feature in either the compatible or incompatible * feature set, it must abort and not try to meddle with @@ -135,8 +134,7 @@ typedef struct ext2_superblock u32 s_reserved[190]; /* Padding to the end of the block */ } ext2_sb_t; -typedef struct ext2_group_descriptor -{ +typedef struct ext2_group_descriptor { u32 bg_block_bitmap; u32 bg_inode_bitmap; u32 bg_inode_table; @@ -153,8 +151,7 @@ typedef struct ext2_group_descriptor #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) -typedef struct ext2_inode -{ +typedef struct ext2_inode { u16 i_mode; u16 i_uid; u32 i_size; @@ -176,8 +173,7 @@ typedef struct ext2_inode } ext2_inode_t; #define EXT2_NAME_LEN 255 -typedef struct ext2_dir_ent -{ +typedef struct ext2_dir_ent { u32 inode; u16 rec_len; u8 name_len; @@ -189,8 +185,7 @@ typedef struct ext2_dir_ent * Ext2 目录类型. * 到目前为止只有低3位有效. */ -enum -{ +enum { EXT2_FT_UNKNOWN, EXT2_FT_REG_FILE, EXT2_FT_DIR, @@ -211,4 +206,4 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode); void ext2_read_file(const ext2_inode_t *inode, char *buf); void ext2_read_data(const ext2_inode_t *inode, unsigned int offset, size_t size, char *buf); -#endif //_EXT2_H +#endif //_EXT2_H diff --git a/include/fcntl.h b/include/fcntl.h index 5aee2cd..fbdeb7d 100644 --- a/include/fcntl.h +++ b/include/fcntl.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: fcntl.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 16:24:15 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -28,4 +28,4 @@ #define O_FSYNC O_SYNC #define O_ASYNC 020000 -#endif //_FCNTL_H +#endif //_FCNTL_H diff --git a/include/fs.h b/include/fs.h index 9cf99c6..5e2ca12 100644 --- a/include/fs.h +++ b/include/fs.h @@ -1,20 +1,20 @@ /* *-------------------------------------------------------------------------- * File Name: fs.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Fri Feb 12 22:29:59 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #ifndef _FS_H #define _FS_H -#include #include +#include /* 分区表开始的位置 */ #define PARTS_POS 0x1BE @@ -45,22 +45,15 @@ unsigned int namei(const char *path); #define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK * EXT2_BLOCK_SIZE) -typedef struct chrdev -{ +typedef struct chrdev { int (*read)(char *buf, size_t count); } chrdev_t; -enum -{ - CHRDEV_CNSL, - CHRDEV_SIZE -}; +enum { CHRDEV_CNSL, CHRDEV_SIZE }; extern chrdev_t *chrdev[]; -typedef struct -{ - +typedef struct { } file_t; #if 0 @@ -114,4 +107,4 @@ static inline pInode find_empty_inode() } #endif -#endif //_FS_H +#endif //_FS_H diff --git a/include/global.h b/include/global.h index 52b4e1a..ea5f0ce 100644 --- a/include/global.h +++ b/include/global.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: global.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jan 23 14:12:40 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -17,4 +17,4 @@ #define __STRING(x) #x #endif -#endif //_GLOBAL_H +#endif //_GLOBAL_H diff --git a/include/i8259.h b/include/i8259.h index 048b6b9..c18fa12 100644 --- a/include/i8259.h +++ b/include/i8259.h @@ -1,25 +1,25 @@ /* *-------------------------------------------------------------------------- * File Name: i8259.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Nov 9 11:37:09 2008 * Version: 1.1 * Last Update: Tue Feb 10 20:28:47 2009 - * + * *-------------------------------------------------------------------------- */ #ifndef _I8259_H #define _I8259_H -#include "irq.h" #include "io.h" +#include "irq.h" #define PIC_MASTER_CMD 0x20 #define PIC_MASTER_IMR 0x21 @@ -29,7 +29,7 @@ #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 @@ -195,4 +195,4 @@ _PORT 0x20 : 0xA0_ }}} #endif -#endif //_I8259_H +#endif //_I8259_H diff --git a/include/io.h b/include/io.h index abd45f7..9566a24 100644 --- a/include/io.h +++ b/include/io.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: io.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Mon Jun 18 23:25:23 2007 * Last Update: Mon Jun 18 23:25:23 2007 - * + * *-------------------------------------------------------------------------- */ @@ -19,75 +19,48 @@ #include -#define inb(port) ( \ - { \ - u8 _bt; \ - asm("inb %%dx,%%al" \ - : "=a"(_bt) \ - : "d"(port)); \ - _bt; \ +#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 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 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 outl(value, port) ({ __asm__("outl %%eax,%%dx" : : "a"(value), "d"(port)); }) -#define outb_p(value, port) ({ \ - __asm__("outb %%al,%%dx;nop;nop;nop;nop" \ - : \ - : "a"(value), "d"(port)); \ -}) +#define outb_p(value, port) ({ __asm__("outb %%al,%%dx;nop;nop;nop;nop" : : "a"(value), "d"(port)); }) -#define inb_p(port) ( \ - { \ - u8 _bt; \ - __asm__("inb %%dx,%%al;nop;nop;nop;nop" \ - : "=a"(_bt) \ - : "d"(port)); \ - _bt; \ +#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)); \ +#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) BUILDIO(l, int) -#endif //_IO_H +#endif //_IO_H diff --git a/include/irq.h b/include/irq.h index bcaaf1b..f59b776 100644 --- a/include/irq.h +++ b/include/irq.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: irq.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Nov 28 16:38:25 2008 * Last Update: Fri Nov 28 16:38:25 2008 - * + * *-------------------------------------------------------------------------- */ @@ -22,25 +22,22 @@ #define FIRST_IRQ_VECT 0x20 #define NR_IRQS (0xFF - FIRST_IRQ_VECT) -typedef struct irq_chip -{ +typedef struct irq_chip { const char *name; 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); +typedef struct irqaction { + // void (*handler)(pt_regs_t * regs, unsigned int irq); void (*handler)(unsigned int irq, pt_regs_t *regs, void *dev_id); const char *dev_name; void *dev_id; struct irqaction *next; } irq_action_t; -typedef struct irq_desc -{ +typedef struct irq_desc { irq_chip_t *chip; irq_action_t *action; unsigned int status; @@ -51,10 +48,8 @@ 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); + // 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,15 +57,12 @@ 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_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"); \ +#define irq_restore(x) \ + do { \ + typecheck(unsigned long, x); \ + __asm__ __volatile__("pushl %0; popfl" ::"g"(x) : "memory", "cc"); \ } while (0) -#endif //_IRQ_H +#endif //_IRQ_H diff --git a/include/kernel.h b/include/kernel.h index 70d7d2c..6de2bc8 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -9,5 +9,5 @@ #pragma once -#include #include +#include diff --git a/include/linkage.h b/include/linkage.h index d672128..e40bdba 100644 --- a/include/linkage.h +++ b/include/linkage.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: linkage.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Jul 10 11:19:26 2009 * Last Update: Fri Jul 10 11:19:26 2009 - * + * *-------------------------------------------------------------------------- */ #ifndef __LINKAGE_H diff --git a/include/list.h b/include/list.h index 66d8ebe..475e7f3 100644 --- a/include/list.h +++ b/include/list.h @@ -1,24 +1,23 @@ /* *-------------------------------------------------------------------------- * File Name: list.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Mon Apr 20 20:52:05 2009 * Last Update: Mon Apr 20 20:52:05 2009 - * + * *-------------------------------------------------------------------------- */ #pragma once /* Allmost Copy From Linux */ -typedef struct list_head -{ +typedef struct list_head { struct list_head *prev, *next; } list_head_t; @@ -26,74 +25,50 @@ typedef struct list_head typedef list_head_t ListHead, *pListHead; #define LIST_HEAD_INIT(name) \ - { \ - &(name), &(name) \ - } + { &(name), &(name) } #define LIST_HEAD(name) list_head_t name = LIST_HEAD_INIT(name) #define INIT_LIST_HEAD(ptr) \ - do \ - { \ + 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_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_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)) + &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) -{ +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; } -static inline void list_add(list_head_t *pnew, list_head_t *head) -{ - _list_add(pnew, head, head->next); -} +static inline void list_add(list_head_t *pnew, list_head_t *head) { _list_add(pnew, head, head->next); } -static inline void list_add_tail(list_head_t *pnew, list_head_t *head) -{ - _list_add(pnew, head->prev, head); -} +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) -{ +static inline void _list_del(list_head_t *prev, list_head_t *next) { next->prev = prev; prev->next = next; } -static inline void list_del(list_head_t *entry) -{ - _list_del(entry->prev, entry->next); -} +static inline void list_del(list_head_t *entry) { _list_del(entry->prev, entry->next); } -static inline void list_del_init(list_head_t *entry) -{ +static inline void list_del_init(list_head_t *entry) { _list_del(entry->prev, entry->next); INIT_LIST_HEAD(entry); } -static inline int list_empty(list_head_t *head) -{ - return head->next == head; -} +static inline int list_empty(list_head_t *head) { return head->next == head; } diff --git a/include/msr.h b/include/msr.h index 0a80e2d..41ee5cd 100644 --- a/include/msr.h +++ b/include/msr.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: msr.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Jan 2 19:54:18 2009 * Last Update: Fri Jan 2 19:54:18 2009 - * + * *-------------------------------------------------------------------------- */ @@ -22,9 +22,8 @@ #define MSR_SYSENTER_EIP 0x176 #define wrmsr(msr, lowval, highval) \ - do \ - { \ + do { \ asm("wrmsr;" ::"c"(msr), "a"(lowval), "d"(highval)); \ } while (0); -#endif //_MSR_H +#endif //_MSR_H diff --git a/include/page.h b/include/page.h index 7d84f70..28bb569 100644 --- a/include/page.h +++ b/include/page.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: page.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sat Feb 7 21:47:42 2009 * Last Update: Sat Feb 7 21:47:42 2009 - * + * *-------------------------------------------------------------------------- */ @@ -29,8 +29,8 @@ #define PAGE_PTE_CNT 1024 #ifndef ASM -#include #include +#include #define get_npd(addr) (((u32)(addr)) >> 22) #define get_npt(addr) ((((u32)(addr)) >> 12) & 0x3FF) @@ -70,16 +70,14 @@ typedef unsigned long pte_t; typedef unsigned int gfp_t; -enum page_flags -{ +enum page_flags { PG_Private, }; struct kmem_cache; typedef struct kmem_cache kmem_cache_t; -typedef struct page -{ +typedef struct page { unsigned long count; unsigned long flags; unsigned long private; @@ -89,7 +87,7 @@ typedef struct page struct page *head_page; unsigned int order; - void **freelist; // for slub + void **freelist; // for slub kmem_cache_t *cache; unsigned long inuse; @@ -102,35 +100,22 @@ 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 -{ +typedef struct free_area { unsigned long free_count; list_head_t free_list; } free_area_t; @@ -140,8 +125,7 @@ void free_pages(unsigned long addr); #define alloc_one_page(gfp_mask) alloc_pages(gfp_mask, 0) -struct kmem_cache -{ +struct kmem_cache { const char *name; unsigned long objsize; @@ -159,18 +143,16 @@ struct kmem_cache }; // TODO Remove -typedef struct page_ -{ - //struct page *prev, *next; +typedef struct page_ { + // struct page *prev, *next; ListHead list; unsigned int order; unsigned int mapNR; unsigned int count; } Page, *pPage; -typedef struct free_area_ -{ - //struct page *prev, *next; +typedef struct free_area_ { + // struct page *prev, *next; ListHead freeList; unsigned char *map; unsigned int mapSize; @@ -179,9 +161,9 @@ typedef struct free_area_ pPage old_alloc_pages(unsigned int order); void old_free_pages(pPage page); -//void free_pages(pPage page, unsigned int order); +// void free_pages(pPage page, unsigned int order); void disp_free_area(); -#endif // ASM +#endif // ASM -#endif //_PAGE_H +#endif //_PAGE_H diff --git a/include/pci.h b/include/pci.h index 4c48b32..cf23ee0 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: pci.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Mar 8 21:32:16 2009 * Last Update: Sun Mar 8 21:32:16 2009 - * + * *-------------------------------------------------------------------------- */ @@ -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 /* @@ -46,7 +46,7 @@ * | Base Address Register 5 | * +-------------------------------------------------------+ 28H * | CardBus CIS Pointer | - * +---------------------------+---------------------------+ 2CH + * +---------------------------+---------------------------+ 2CH * | System ID | Subsystem ID | * +---------------------------+---------------------------+ 30H * | Expansion ROM Base Address | @@ -64,8 +64,7 @@ extern list_head_t pci_devs; #define BARS_CNT 6 -typedef struct pci_device -{ +typedef struct pci_device { list_head_t list; unsigned int bus, dev, devfn; @@ -77,7 +76,7 @@ typedef struct pci_device unsigned int progif; unsigned int classcode; unsigned int hdr_type; - //unsigned int bar0, bar1, bar2, bar3, bar4, bar5; + // unsigned int bar0, bar1, bar2, bar3, bar4, bar5; unsigned int bars[BARS_CNT]; unsigned int sub_system_id; unsigned int system_id; @@ -182,10 +181,7 @@ typedef union pci_device 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) -{ - return PCI_CMD(pci->bus, pci->dev, pci->devfn, reg); -} +static inline u32 pci_cmd(pci_device_t *pci, unsigned int reg) { return PCI_CMD(pci->bus, pci->dev, pci->devfn, reg); } int pci_read_config_byte(int cmd); int pci_read_config_word(int cmd); @@ -221,7 +217,7 @@ void pci_write_config_long(int value, int cmd); * | Prefetchable Memory Limit | Prefetchable Memory Base | * +---------------------------+---------------------------+ 28H * | Prefetchable Base Upper 32bit | - * +---------------------------+---------------------------+ 2CH + * +---------------------------+---------------------------+ 2CH * | Prefetchable Limit Upper 32bit | * +---------------------------+---------------------------+ 30H * | IO Limit Upper 16bit | IO Base Upper 16bit | diff --git a/include/printk.h b/include/printk.h index dc4ed3c..a1acab0 100644 --- a/include/printk.h +++ b/include/printk.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: printk.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Mar 4 21:49:19 2009 * Last Update: Wed Mar 4 21:49:19 2009 - * + * *-------------------------------------------------------------------------- */ @@ -26,8 +26,7 @@ int printlo(unsigned int line, unsigned int offset, const char *fmtstr, ...); #define printlr(line, fmt, args...) printlo(line, 40, fmt, ##args) // monitor print line -enum -{ +enum { MPL_TITLE, MPL_ROOTDEV, MPL_CLOCK, diff --git a/include/processor.h b/include/processor.h index 033f403..a35db2c 100644 --- a/include/processor.h +++ b/include/processor.h @@ -1,39 +1,39 @@ /* *-------------------------------------------------------------------------- * File Name: processor.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Aug 03 21:37:34 2008 * Last Update: Sun Aug 03 21:37:34 2008 - * + * *-------------------------------------------------------------------------- */ #ifndef _DESCRIPTOR_H #define _DESCRIPTOR_H -#include "types.h" -#include "system.h" #include "string.h" +#include "system.h" +#include "types.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. -//we just need Accessed. -//For Data Segment. +// 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. +// 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_RD (DA_A | DA_ED) // down to ... #define DSA_RWD (DA_A | DA_W | DA_ED) -//For Code Segment. +// 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) @@ -43,23 +43,22 @@ // 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 S : 1; // allways set to 0. NOT SYS SEG DESC unsigned char DPL : 2; - unsigned char P : 1; //allways set to 1. + 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 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; @@ -67,14 +66,13 @@ typedef struct // TSS State,LDT // And Gates(Call,Interrupt,Trap,Tss) Descriptor. //------------------------------------------------------------------------- -typedef struct -{ +typedef struct { unsigned short eaddrL; unsigned short selector; - unsigned char parmeter : 5; //for call gate - //reserved by other gates. + unsigned char parmeter : 5; // for call gate + // reserved by other gates. unsigned char zero : 3; unsigned char type : 4; unsigned char S : 1; @@ -85,9 +83,8 @@ typedef struct } Gate, *pGate; -//just used for type... -typedef union -{ +// just used for type... +typedef union { Seg seg; Gate gate; } Desc, *pDesc; @@ -99,21 +96,18 @@ extern Desc idt[NIDT]; extern Desc gdt[NGDT]; //------------------------------------------------------------------------- -//Define Gate Types... +// Define Gate Types... //------------------------------------------------------------------------- -#define INTR_GATE 0x0E //Clear 'IF' bit.---->Close Interrupt -#define TRAP_GATE 0x0F //Keep 'IF' bit. +#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) - asm("xchg %bx,%bx"); +static inline void _init_desc(pDesc 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) -{ +static inline Desc _create_seg(u8 type, u8 DPL) { Desc d; pSeg p = &d.seg; @@ -130,8 +124,7 @@ static inline Desc _create_seg(u8 type, u8 DPL) return d; } -static inline Desc _create_gate(u32 handler, u8 type, u8 DPL) -{ +static inline Desc _create_gate(u32 handler, u8 type, u8 DPL) { Desc d; pGate p = &d.gate; @@ -146,20 +139,15 @@ static inline Desc _create_gate(u32 handler, u8 type, u8 DPL) return d; } -static inline void set_idt_gate(u32 vec, u32 handler, u8 type, u8 DPL) -{ - idt[vec] = _create_gate(handler, type, 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 \ - { \ + 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; @@ -187,8 +175,7 @@ typedef struct u16 ldt, _ldt; u16 T : 1, _T : 15, iomap_base; } TSS, *pTSS; -static inline void set_tss_gate(u32 vec, u32 addr) -{ +static inline void set_tss_gate(u32 vec, u32 addr) { #if 1 pSeg p = (pSeg)(gdt + vec); _init_desc((pDesc)p); @@ -208,4 +195,4 @@ static inline void set_tss_gate(u32 vec, u32 addr) extern TSS tss; -#endif //_DESCRIPTOR_H +#endif //_DESCRIPTOR_H diff --git a/include/sched.h b/include/sched.h index f5a87b1..175a87b 100644 --- a/include/sched.h +++ b/include/sched.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: sched.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sat Feb 7 21:43:49 2009 * Last Update: Sat Feb 7 21:43:49 2009 - * + * *-------------------------------------------------------------------------- */ diff --git a/include/semaphore.h b/include/semaphore.h index 8f1343b..565a3f4 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -9,24 +9,19 @@ #pragma once +#include #include #include -#include -typedef struct semaphore -{ +typedef struct semaphore { volatile unsigned int cnt; 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) \ - semaphore_t name = SEMAPHORE_INITIALIZER(name, 1) +#define DECLARE_MUTEX(name) semaphore_t name = SEMAPHORE_INITIALIZER(name, 1) void down(semaphore_t *s); void up(semaphore_t *s); diff --git a/include/stat.h b/include/stat.h index 2344166..01cf303 100644 --- a/include/stat.h +++ b/include/stat.h @@ -1,20 +1,19 @@ /* *-------------------------------------------------------------------------- * File Name: stat.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Fri Feb 12 18:01:27 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #ifndef _STAT_H #define _STAT_H -typedef struct stat -{ +typedef struct stat { unsigned long st_dev; unsigned long st_ino; unsigned short st_mode; @@ -55,4 +54,4 @@ typedef struct stat #define S_ISFIFO(m) (((m)&S_IFMT) == S_IFIFO) #define S_ISSOCK(m) (((m)&S_IFMT) == S_IFSOCK) -#endif //_STAT_H +#endif //_STAT_H diff --git a/include/stdio.h b/include/stdio.h index 203b66d..1b36e1d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: stdio.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Mon Mar 9 01:59:06 2009 * Last Update: Mon Mar 9 01:59:06 2009 - * + * *-------------------------------------------------------------------------- */ @@ -19,15 +19,14 @@ #include #include extern int write(int fd, const char *buf, unsigned long size); -static inline int printf(const char *fmt, ...) -{ +static inline int printf(const char *fmt, ...) { char ptfbuf[512]; char *args = (char *)(((char *)&fmt) + 4); vsprintf(ptfbuf, fmt, args); - //asm("xchg %bx,%bx;"); + // asm("xchg %bx,%bx;"); return write(0, ptfbuf, strlen(ptfbuf)); } -#endif //_STDIO_H +#endif //_STDIO_H diff --git a/include/stdlib.h b/include/stdlib.h index 17f4008..f368d9d 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: stdlib.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Wed Feb 17 19:01:02 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -15,4 +15,4 @@ extern int atoi(const char *s); -#endif //_STDLIB_H +#endif //_STDLIB_H diff --git a/include/string.h b/include/string.h index 2c7605b..9cb3205 100644 --- a/include/string.h +++ b/include/string.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: string.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Jul 30 16:03:27 2008 * Last Update: Wed Jul 30 16:03:27 2008 - * + * *-------------------------------------------------------------------------- */ @@ -29,4 +29,4 @@ 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); -#endif //_STRING_H +#endif //_STRING_H diff --git a/include/syscall.h b/include/syscall.h index 59b4a83..43ed6d0 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: syscall.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Fri Jan 2 19:52:28 2009 * Last Update: Tue Feb 23 02:32:35 2010 - * + * *-------------------------------------------------------------------------- */ @@ -21,8 +21,8 @@ #ifndef ASM -#include "page.h" #include "errno.h" +#include "page.h" int _syscall0(int nr); int _syscall1(int nr, unsigned long a); @@ -36,7 +36,7 @@ int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigne #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) -#endif // ASM +#endif // ASM #define SYSC_WRITE (0) #define SYSC_REBOOT (1) @@ -53,4 +53,4 @@ int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigne #define SYSC_DEBUG (12) #define SYSC_BAD_NR (SYSC_NUM - 1) -#endif //_SYSCALL_H +#endif //_SYSCALL_H diff --git a/include/system.h b/include/system.h index 24f16ff..8c0de38 100644 --- a/include/system.h +++ b/include/system.h @@ -1,24 +1,24 @@ /* *-------------------------------------------------------------------------- * File Name: system.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sat Feb 7 18:57:58 2009 * Last Update: Sat Feb 7 18:57:58 2009 - * + * *-------------------------------------------------------------------------- */ #ifndef _SYSTEM_H #define _SYSTEM_H -#include #include +#include #define KRNLADDR PAGE_OFFSET #define PT_REGS_EBX 0 @@ -40,33 +40,29 @@ #define PT_REGS_SS 64 #ifndef ASM -#include "types.h" #include "printk.h" +#include "types.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; \ - }) +#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) \ - ; \ +#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; @@ -89,8 +85,7 @@ extern char gdtr[6], idtr[6]; #define INT_STACK_SIZE PAGE_SIZE -enum GDTSelectorIndex -{ +enum GDTSelectorIndex { INDEX_SPACE = 0, INDEX_KCODE, INDEX_KDATA, @@ -107,21 +102,19 @@ enum GDTSelectorIndex INDEX_TSS, }; -typedef struct pt_regs -{ +typedef struct pt_regs { u32 ebx; u32 ecx; u32 edx; - u32 esi; u32 edi; + u32 esi; u32 ebp; u32 eax; u16 ds, _ds; u16 es, _es; u16 fs, _fs; u16 gs, _gs; - union - { + union { u32 irq; u32 errcode; }; @@ -134,14 +127,13 @@ typedef struct pt_regs typedef unsigned long dev_t; -typedef struct system -{ +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; @@ -245,4 +237,4 @@ void system_delay(); #define KRNL_INIT_STACK_SIZE 4096 -#endif //_SYSTEM_H +#endif //_SYSTEM_H diff --git a/include/task.h b/include/task.h index aa0128d..be0b525 100644 --- a/include/task.h +++ b/include/task.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: task.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Thu Dec 31 16:54:48 2009 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -18,15 +18,14 @@ #define TI_preempt_cnt 0 #ifndef ASM -#include +#include #include -#include +#include #include #include -#include +#include -enum -{ +enum { TASK_UNUSED, TASK_RUNNING, TASK_WAIT, @@ -36,15 +35,12 @@ enum #define TASK_NAME_SIZE 32 -typedef struct wait_queue_head -{ +typedef struct wait_queue_head { list_head_t task_list; } wait_queue_head_t; -typedef union task_union -{ - struct - { +typedef union task_union { + struct { unsigned long preempt_cnt; unsigned long esp0; /* kernel stack */ @@ -70,7 +66,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]; @@ -78,21 +74,15 @@ typedef union task_union task_union *alloc_task_union(); -static inline task_union *get_current() -{ +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; } #define current get_current() -static inline pid_t sysc_getpid() -{ - return current->pid; -} +static inline pid_t sysc_getpid() { return current->pid; } task_union *find_task(pid_t pid); @@ -104,4 +94,4 @@ task_union *find_task(pid_t pid); #define del_tsk_from_list(tsk) list_del((&tsk->list)) #endif -#endif //_TASK_H +#endif //_TASK_H diff --git a/include/types.h b/include/types.h index a07163d..0c9d5ec 100644 --- a/include/types.h +++ b/include/types.h @@ -1,16 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: types.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Aug 13 23:06:22 2008 * Last Update: Wed Aug 13 23:06:22 2008 - * + * *-------------------------------------------------------------------------- */ @@ -47,15 +47,11 @@ typedef unsigned long mode_t; #define NULL ((void *)0) -typedef enum -{ - false, - true -} bool; +typedef enum { false, true } bool; //========================================================================= -//Define kinds of function's type ... +// Define kinds of function's type ... //========================================================================= typedef void (*pf_intr)(); -#endif //_TYPES_H +#endif //_TYPES_H diff --git a/include/unistd.h b/include/unistd.h index 08119b2..77d7e8f 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: unistd.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sun Feb 7 13:24:11 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -18,4 +18,4 @@ extern pid_t fork(); extern int exit(int status); -#endif //_UNISTD_H +#endif //_UNISTD_H diff --git a/include/wait.h b/include/wait.h index 78af596..9e0430f 100644 --- a/include/wait.h +++ b/include/wait.h @@ -1,43 +1,35 @@ /* *-------------------------------------------------------------------------- * File Name: wait.h - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Mon Feb 22 20:50:56 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #pragma once -#include #include +#include #include -typedef struct -{ +typedef struct { task_union *task; 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) \ - wait_queue_head_t name = WAIT_QUEUE_HEAD_INITIALIZER(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) \ - wait_queue_t name = WAIT_QUEUE_INITIALIZER(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 add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq); diff --git a/kernel/assert.c b/kernel/assert.c index 29f8b94..e723cdd 100644 --- a/kernel/assert.c +++ b/kernel/assert.c @@ -1,19 +1,17 @@ /* *-------------------------------------------------------------------------- * File Name: assert.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jan 23 15:25:29 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -void assert_fail(char *exp, char *file, unsigned int line, char *func) -{ - printk("%s:%d: %s: Assertion \'%s\' failed.\n", - file, line, func, exp); +void assert_fail(char *exp, char *file, unsigned int line, char *func) { + printk("%s:%d: %s: Assertion \'%s\' failed.\n", file, line, func, exp); while (1) ; diff --git a/kernel/clock.c b/kernel/clock.c index 61a7772..5dc04ca 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: clock.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Jan 5 09:51:54 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -15,15 +15,11 @@ static unsigned int jiffies = 0; -unsigned int sys_clock() -{ - return jiffies; -} +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++; - //printd("^"); - //printl(MPL_CLOCK, "clock irq: %d", jiffies); + // printd("^"); + // printl(MPL_CLOCK, "clock irq: %d", jiffies); } diff --git a/kernel/cpuid.c b/kernel/cpuid.c index e095f49..bde3866 100644 --- a/kernel/cpuid.c +++ b/kernel/cpuid.c @@ -1,49 +1,38 @@ /* *-------------------------------------------------------------------------- * File Name: cpuid.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sat Feb 28 14:43:35 2009 * Last Update: Sat Feb 28 14:43:35 2009 - * + * *-------------------------------------------------------------------------- */ #include -#define TEST_FEATURE(val, bit, fea) \ - do \ - { \ - if (ISSET_BIT(val, bit)) \ - printk(" %s", fea); \ +#define TEST_FEATURE(val, bit, fea) \ + do { \ + if (ISSET_BIT(val, bit)) printk(" %s", fea); \ } while (0); -typedef struct reg -{ +typedef struct reg { unsigned long eax, ebx, ecx, edx; } reg_t; -reg_t cpuid(unsigned long op) -{ +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)); + asm("cpuid;" : "=a"(r.eax), "=b"(r.ebx), "=c"(r.ecx), "=d"(r.edx) : "a"(op)); return r; } -void detect_cpu() -{ - +void detect_cpu() { reg_t r; - unsigned short int cpu_sn[6]; //serial number + unsigned short int cpu_sn[6]; // serial number int i; /**********************Get CPU Name********************************/ @@ -57,7 +46,7 @@ void detect_cpu() printk("%s ", cpu_name); /**********************Get Processor Brand String******************/ - char pbs[50]; //processor brand string + char pbs[50]; // processor brand string r = cpuid(0x80000002); memcpy(pbs + 0, &r.eax, 4); memcpy(pbs + 4, &r.ebx, 4); @@ -77,7 +66,7 @@ void detect_cpu() printk("%s", pbs); /**********************Get Number of Processors********************/ - int pn; //number of logical processors in one physical processor + int pn; // number of logical processors in one physical processor r = cpuid(1); pn = ((r.ebx & 0x00FF0000) >> 16); printk(" x %d Cores\n", pn); @@ -103,23 +92,22 @@ void detect_cpu() TEST_FEATURE(fv, 17, "pse-36") TEST_FEATURE(fv, 18, "psn") TEST_FEATURE(fv, 19, "clflush") - //TEST_FEATURE(fv, 20, "Reserved") + // TEST_FEATURE(fv, 20, "Reserved") TEST_FEATURE(fv, 21, "dts") TEST_FEATURE(fv, 22, "acpi") TEST_FEATURE(fv, 23, "mmx") TEST_FEATURE(fv, 24, "fxsr") TEST_FEATURE(fv, 25, "sse") TEST_FEATURE(fv, 26, "sse2") - //TEST_FEATURE(fv, 27, "Reserved") + // TEST_FEATURE(fv, 27, "Reserved") TEST_FEATURE(fv, 28, "ht") TEST_FEATURE(fv, 29, "tm") - //TEST_FEATURE(fv, 30, "Reserved") + // TEST_FEATURE(fv, 30, "Reserved") TEST_FEATURE(fv, 31, "pbe") printk("\n"); - if (!((1UL << 11) & fv)) - { + if (!((1UL << 11) & fv)) { printk("Your CPU Do Not Support SYSENTER/SYSEXIT\n"); while (1) ; diff --git a/kernel/exec.c b/kernel/exec.c index 3f1ead9..4f33469 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -1,31 +1,30 @@ /* *-------------------------------------------------------------------------- * File Name: exec.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Feb 20 21:12:30 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include -#include -#include #include -#include -#include -#include #include -#include #include +#include +#include +#include #include +#include +#include +#include +#include extern void *syscall_exit; -void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) -{ +void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) { assert(PAGE_ALIGN(vaddr) == vaddr); assert(PAGE_ALIGN(paddr) == paddr); @@ -35,8 +34,7 @@ void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) 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); memset(page_table, 0, PAGE_SIZE); page_table = (pte_t *)va2pa(page_table); @@ -48,13 +46,11 @@ void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) page_table[npte] = paddr | flags; } -int sysc_exec(const char *path, char *const argv[]) -{ - assert(argv == NULL); // unsupport now +int sysc_exec(const char *path, char *const argv[]) { + assert(argv == NULL); // unsupport now unsigned int ino = namei(path); - if (ino == 0) - return -ENOENT; + if (ino == 0) return -ENOENT; ext2_inode_t inode; @@ -68,22 +64,19 @@ int sysc_exec(const char *path, char *const argv[]) printk("Elf Entry: %08x\n", ehdr->e_entry); int i, j; - for (i = 0; i < ehdr->e_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)); - 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); + 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); unsigned long vaddr = phdr->p_vaddr; unsigned long offset = phdr->p_offset; unsigned long mmsz = phdr->p_memsz; unsigned long filesz = phdr->p_filesz; - if (phdr->p_type != PT_LOAD) - continue; + if (phdr->p_type != PT_LOAD) continue; assert(mmsz >= filesz); @@ -96,11 +89,9 @@ int sysc_exec(const char *path, char *const argv[]) 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; j < pgcnt; ++j) - { + for (j = 0; j < pgcnt; ++j) { put_paging(vaddr + j * PAGE_SIZE, (unsigned long)(va2pa(buf)) + j * PAGE_SIZE, 7); } } @@ -123,14 +114,14 @@ int sysc_exec(const char *path, char *const argv[]) #endif regs->eip = (unsigned long)ehdr->e_entry; regs->edx = regs->eip; - regs->ecx = KRNLADDR; //(0xC0000000 - 16); + regs->ecx = KRNLADDR; //(0xC0000000 - 16); - //kfree(buf); + // kfree(buf); free_pages((unsigned long)ehdr); // TODO FIXME - //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/exit.c b/kernel/exit.c index 9459879..44c136f 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: exit.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Thu Mar 4 10:03:57 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ @@ -14,8 +14,7 @@ #include #include -int sysc_exit(int status) -{ +int sysc_exit(int status) { unsigned long flags; irq_save(flags); current->state = TASK_EXITING; diff --git a/kernel/fork.c b/kernel/fork.c index fd3d368..5caa1ca 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1,36 +1,33 @@ /* *-------------------------------------------------------------------------- * File Name: fork.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sun Feb 7 13:25:28 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include #include +#include -int sysc_fork(pt_regs_t regs) -{ - return do_fork(®s, 0); -} +int sysc_fork(pt_regs_t regs) { return do_fork(®s, 0); } extern void ret_from_fork_user(); extern void ret_from_fork_krnl(); extern pid_t get_next_pid(); extern list_head_t all_tasks; -int do_fork(pt_regs_t *regs, unsigned long flags) -{ +int do_fork(pt_regs_t *regs, unsigned long flags) { 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)); strcpy(tsk->name, "init"); @@ -46,29 +43,25 @@ int do_fork(pt_regs_t *regs, unsigned long flags) memcpy((void *)tsk->cr3, (void *)current->cr3, PAGE_SIZE); - for (i = 0; i < PAGE_PDE_CNT; ++i) - { + for (i = 0; i < PAGE_PDE_CNT; ++i) { unsigned long spde = (unsigned long)pde_src[i]; unsigned long dpde = 0; - if (i >= 768) - { + if (i >= 768) { pde_dst[i] = pde_src[i]; continue; } - if (pde_src[i] == 0) + if (pde_src[i] == 0) { continue; + } - if (PAGE_ALIGN(spde) != 0) - { + 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 - { + } else { pde_dst[i] = 0; continue; } @@ -76,13 +69,13 @@ int do_fork(pt_regs_t *regs, unsigned long flags) pte_t *pte_src = pa2va(PAGE_ALIGN(spde)); pte_t *pte_dst = pa2va(PAGE_ALIGN(dpde)); - for (j = 0; j < PAGE_PTE_CNT; ++j) - { + for (j = 0; j < PAGE_PTE_CNT; ++j) { pte_src[j] &= ~PAGE_WR; pte_dst[j] = pte_src[j]; - if (pte_src[j] == 0) + 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++; @@ -100,14 +93,13 @@ int do_fork(pt_regs_t *regs, unsigned long flags) *child_regs = *regs; child_regs->eax = 0; - child_regs->eflags |= 0x200; //enable IF + 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; - if (flags & FORK_KRNL) - { + if (flags & FORK_KRNL) { tsk->eip = (unsigned long)ret_from_fork_krnl; } diff --git a/kernel/i8259.c b/kernel/i8259.c index 6a7e5cf..69c4e37 100644 --- a/kernel/i8259.c +++ b/kernel/i8259.c @@ -1,45 +1,44 @@ /* *-------------------------------------------------------------------------- * File Name: i8259.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Nov 9 11:35:22 2008 * Last Update: Tue Feb 10 22:37:15 2009 - * + * *-------------------------------------------------------------------------- */ -#include "io.h" #include "i8259.h" + +#include "io.h" #include "irq.h" -void mask_i8259() -{ - //mask all of 8259 +void mask_i8259() { + // mask all of 8259 outb_p(0xFF, PIC_MASTER_IMR); outb_p(0xFF, PIC_SLAVE_IMR); } -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 << PIC_CASCADE_IR), PIC_MASTER_IMR); //IR2 Connect Slave. +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 << PIC_CASCADE_IR), PIC_MASTER_IMR); // IR2 Connect Slave. #if PIC_AEOI outb_p(0x03, PIC_MASTER_IMR); #else - outb_p(0x01, PIC_MASTER_IMR); // Normal EOI + outb_p(0x01, PIC_MASTER_IMR); // Normal EOI #endif - //Slave... + // Slave... outb_p(0x11, PIC_SLAVE_CMD); - outb_p(0x28, PIC_SLAVE_IMR); // IR0-7 mapped to 0x28-0x2F + outb_p(0x28, PIC_SLAVE_IMR); // IR0-7 mapped to 0x28-0x2F outb_p(PIC_CASCADE_IR, PIC_SLAVE_IMR); #if PIC_AEOI outb_p(0x03, PIC_SLAVE_IMR); @@ -49,45 +48,36 @@ void init_i8259() mask_i8259(); } -int enable_i8259_irq(unsigned int irq) -{ +int enable_i8259_irq(unsigned int irq) { unsigned char mask = 0; - if (irq & 8) - { + if (irq & 8) { mask = ~(1 << (irq - 8)); mask &= inb(PIC_SLAVE_IMR); outb(mask, PIC_SLAVE_IMR); - } - else - { + } else { mask = ~(1 << irq); mask &= inb(PIC_MASTER_IMR); outb_p(mask, PIC_MASTER_IMR); } } -int disable_i8259_irq(unsigned int irq) -{ +int disable_i8259_irq(unsigned int irq) { unsigned char mask = 0; - if (irq & 8) - { + if (irq & 8) { mask |= (1 << (irq - 8)); mask |= inb(PIC_SLAVE_IMR); outb(mask, PIC_SLAVE_IMR); - } - else - { + } else { mask |= (1 << irq); mask |= inb(PIC_MASTER_IMR); outb(mask, PIC_MASTER_IMR); } } -void mask_ack_i8259_irq(unsigned int irq) -{ +void mask_ack_i8259_irq(unsigned int irq) { unsigned char mask = 0; - if (irq & 8) // Slave + if (irq & 8) // Slave { mask |= (1 << (irq - 8)); mask |= inb(PIC_SLAVE_IMR); @@ -101,8 +91,7 @@ void mask_ack_i8259_irq(unsigned int irq) // Specific EOI to master outb(0x60 + (PIC_CASCADE_IR & 0x07), PIC_MASTER_CMD); #endif - } - else // Master + } else // Master { mask |= (1 << irq); mask |= inb(PIC_MASTER_IMR); @@ -117,14 +106,11 @@ void mask_ack_i8259_irq(unsigned int irq) } } -irq_chip_t i8259_chip = - { - .name = "XT-PIC", - .enable = enable_i8259_irq, - .disable = disable_i8259_irq, - .ack = mask_ack_i8259_irq, +irq_chip_t i8259_chip = { + .name = "XT-PIC", + .enable = enable_i8259_irq, + .disable = disable_i8259_irq, + .ack = mask_ack_i8259_irq, }; -void do_i8259_IRQ(pt_regs_t *regs, unsigned int irq) -{ -} +void do_i8259_IRQ(pt_regs_t *regs, unsigned int irq) {} diff --git a/kernel/init.c b/kernel/init.c index 73b8fc6..ede85d5 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -1,15 +1,15 @@ +#include +#include #include -#include -#include +#include #include -#include -#include -#include #include -#include -#include +#include #include -#include +#include +#include +#include +#include void root_task_entry(); @@ -24,8 +24,7 @@ int debug_wait_queue_get(); extern void *ring3; extern void *ring3_stack_top; -void user_task_entry() -{ +void user_task_entry() { printk("user_task_entry: %08x %08x\n", ring3_stack_top, &ring3_stack_top); #if 0 asm("sti;sysexit;"::"d"(&ring3), "c"(&ring3_stack_top)); @@ -34,23 +33,20 @@ void user_task_entry() #endif } -void init_task_entry() -{ +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++); - //printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); - int v = 0; //debug_wait_queue_get(); - //printk("task:%d wait queue get %d\n", id, v); + // printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); + int v = 0; // debug_wait_queue_get(); + // printk("task:%d wait queue get %d\n", id, v); } } -int kernel_fork() -{ +int kernel_fork() { int pid = 0; pt_regs_t *regs = ((pt_regs_t *)(TASK_SIZE + ((unsigned long)current))) - 1; @@ -66,32 +62,29 @@ int kernel_fork() return pid; } -void kernel_task(void *entry) -{ +void kernel_task(void *entry) { pt_regs_t regs; memset((void *)®s, 0, sizeof(regs)); regs.edx = (unsigned long)entry; - //int pid = do_fork(®s, FORK_KRNL); - int pid = kernel_fork(); + // int pid = do_fork(®s, FORK_KRNL); + // int pid = kernel_fork(); + int pid = do_fork(®s, FORK_KRNL); printk("kernel task pid is %d\n", pid); enable_irq(); } -void root_task_entry() -{ - +void root_task_entry() { kernel_task(init_task_entry); - //kernel_task(user_task_entry); - //kernel_task(init_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++); - //printk("root:0 [%08x] weight %d cnt %d", current, current->weight, cnt++); + // printk("root:0 [%08x] weight %d cnt %d", current, current->weight, cnt++); asm("sti;hlt;"); sysc_test(); - //syscall0(SYSC_TEST); + // syscall0(SYSC_TEST); } } diff --git a/kernel/innerint.c b/kernel/innerint.c index 3c135da..c8ad001 100644 --- a/kernel/innerint.c +++ b/kernel/innerint.c @@ -1,95 +1,50 @@ /* *-------------------------------------------------------------------------- * File Name: innerint.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Mon Nov 10 15:58:46 2008 * Last Update: Tue Feb 10 22:39:24 2009 * Version: 2.0 * Last Update: Fri Jul 10 11:55:39 2009 - * + * *-------------------------------------------------------------------------- */ -#include #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) \ - ; \ +#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) -{ - DIE_MSG(); -} -void doDebug(pt_regs_t regs) -{ - DIE_MSG(); -} -void doNMI(pt_regs_t regs) -{ - DIE_MSG(); -} -void doBreakPoint(pt_regs_t regs) -{ - DIE_MSG(); -} -void doOverFlow(pt_regs_t regs) -{ - DIE_MSG(); -} -void doBoundsCheck(pt_regs_t regs) -{ - DIE_MSG(); -} -void doInvalidOpcode(pt_regs_t regs) -{ - DIE_MSG(); -} -void doDeviceNotAvailable(pt_regs_t regs) -{ - DIE_MSG(); -} -void doDoubleFault(pt_regs_t regs) -{ - DIE_MSG(); -} -void doCoprocSegOverRun(pt_regs_t regs) -{ - DIE_MSG(); -} -void doInvalidTss(pt_regs_t regs) -{ - DIE_MSG(); -} -void doSegNotPresent(pt_regs_t regs) -{ - DIE_MSG(); -} -void doStackFault(pt_regs_t regs) -{ - DIE_MSG(); -} -void doGeneralProtection(pt_regs_t regs) -{ - DIE_MSG(); -} +void doDivideError(pt_regs_t regs) { DIE_MSG(); } +void doDebug(pt_regs_t regs) { DIE_MSG(); } +void doNMI(pt_regs_t regs) { DIE_MSG(); } +void doBreakPoint(pt_regs_t regs) { DIE_MSG(); } +void doOverFlow(pt_regs_t regs) { DIE_MSG(); } +void doBoundsCheck(pt_regs_t regs) { DIE_MSG(); } +void doInvalidOpcode(pt_regs_t regs) { DIE_MSG(); } +void doDeviceNotAvailable(pt_regs_t regs) { DIE_MSG(); } +void doDoubleFault(pt_regs_t regs) { DIE_MSG(); } +void doCoprocSegOverRun(pt_regs_t regs) { DIE_MSG(); } +void doInvalidTss(pt_regs_t regs) { DIE_MSG(); } +void doSegNotPresent(pt_regs_t regs) { DIE_MSG(); } +void doStackFault(pt_regs_t regs) { DIE_MSG(); } +void doGeneralProtection(pt_regs_t regs) { DIE_MSG(); } void do_no_page(void *); void do_wp_page(void *); -void doPageFault(pt_regs_t regs) -{ +void doPageFault(pt_regs_t regs) { #if 0 US RW P - Description 0 0 0 - Supervisory process tried to read a non-present page entry @@ -101,28 +56,21 @@ US RW P - Description 1 1 0 - User process tried to write to a non-present page entry 1 1 1 - User process tried to write a page and caused a protection fault #endif - //DIE_MSG(); + // DIE_MSG(); 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); + // printk("do page fault errcode %x addr %08x [%08x]\n", errcode, addr, current); - //assert(errcode != 2 && errcode != 6); + // assert(errcode != 2 && errcode != 6); - if ((errcode & PAGE_P) == 0) - { + if ((errcode & PAGE_P) == 0) { do_no_page(addr); - } - else - { + } else { do_wp_page(addr); } } -void doCoprocError(pt_regs_t regs) -{ - DIE_MSG(); -} +void doCoprocError(pt_regs_t regs) { DIE_MSG(); } diff --git a/kernel/irq.c b/kernel/irq.c index c6579b7..b947705 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -1,49 +1,38 @@ /* *-------------------------------------------------------------------------- * File Name: irq.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Thu Jul 16 18:06:22 2009 * Last Update: Thu Jul 16 18:06:22 2009 - * + * *-------------------------------------------------------------------------- */ -#include -#include #include -#include #include +#include +#include +#include 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; } -irq_chip_t 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}; - -__attribute__((regparm(1))) void irq_handler(pt_regs_t *regs) -{ +irq_chip_t 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}; + +__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; } @@ -55,15 +44,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; } @@ -74,51 +61,35 @@ __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, - void *dev_id) -{ +int request_irq(unsigned int irq, void (*handler)(unsigned int, pt_regs_t *, void *), const char *devname, void *dev_id) { irq_action_t *p; - if (irq >= NR_IRQS) - return -EINVAL; - if (handler == NULL) - return -EINVAL; + if (irq >= NR_IRQS) return -EINVAL; + if (handler == NULL) return -EINVAL; // 检查是否已经注册过处理函数了 p = irq_desc[irq].action; - while (p != NULL) - { - if (p->handler == handler) - return 0; + while (p != NULL) { + if (p->handler == handler) return 0; p = p->next; } p = (irq_action_t *)kmalloc(sizeof(irq_action_t), 0); - if (p == NULL) - return -ENOMEM; + 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) - { + if (irq_desc[irq].action != NULL) { p->next = irq_desc[irq].action; - //printk("p->next:%x\n", p->next); + // printk("p->next:%x\n", p->next); } irq_desc[irq].action = p; - //printk("irq: %d action:%x\n", irq, p); + // printk("irq: %d action:%x\n", irq, p); return 0; } -int open_irq(unsigned int irq) -{ - return irq_desc[irq].chip->enable(irq); -} +int open_irq(unsigned int irq) { return irq_desc[irq].chip->enable(irq); } -int close_irq(unsigned int irq) -{ - return irq_desc[irq].chip->disable(irq); -} +int close_irq(unsigned int irq) { return irq_desc[irq].chip->disable(irq); } diff --git a/kernel/pci.c b/kernel/pci.c index 365a0b9..f6ff165 100644 --- a/kernel/pci.c +++ b/kernel/pci.c @@ -1,84 +1,73 @@ /* *-------------------------------------------------------------------------- * File Name: pci.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Sun Mar 8 21:33:13 2009 * Last Update: Sun Mar 8 21:33:13 2009 - * + * *-------------------------------------------------------------------------- */ #include #include -#include -#include #include +#include +#include LIST_HEAD(pci_devs); const char *pci_get_info(unsigned int classcode, unsigned int progif); -int pci_read_config_byte(int cmd) -{ +int pci_read_config_byte(int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); return inb(PCI_DATA + (PCI_GET_CMD_REG(cmd) & 3)); } -int pci_read_config_word(int cmd) -{ +int pci_read_config_word(int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); return inw(PCI_DATA + (PCI_GET_CMD_REG(cmd) & 2)); } -int pci_read_config_long(int cmd) -{ +int pci_read_config_long(int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); return inl(PCI_DATA); } -void pci_write_config_byte(int value, int cmd) -{ +void pci_write_config_byte(int value, int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); outb(value & 0xFF, PCI_DATA); } -void pci_write_config_word(int value, int cmd) -{ +void pci_write_config_word(int value, int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); outw(value & 0xFFFF, PCI_DATA); } -void pci_write_config_long(int value, int cmd) -{ +void pci_write_config_long(int value, int cmd) { outl(PCI_CONFIG_CMD(cmd), PCI_ADDR); outl(value, PCI_DATA); } -void scan_pci_bus(int bus) -{ +void scan_pci_bus(int bus) { u8 dev, devfn; u32 cmd; u32 v; int i; printk("scanning pci bus %d\n", bus); - for (dev = 0; dev < 32; dev++) - { - for (devfn = 0; devfn < 8; devfn++) - { + for (dev = 0; dev < 32; dev++) { + for (devfn = 0; devfn < 8; devfn++) { cmd = PCI_CMD(bus, dev, devfn, PCI_VENDORID); v = pci_read_config_word(cmd); - if (v == 0xFFFF) - continue; + 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; } @@ -111,8 +100,7 @@ void scan_pci_bus(int bus) pci->hdr_type = pci_read_config_byte(cmd); pci->hdr_type &= PCI_HDRTYPE_MASK; - for (i = 0; i < BARS_CNT; ++i) - { + for (i = 0; i < BARS_CNT; ++i) { cmd = PCI_CMD(bus, dev, devfn, PCI_BAR0 + i * 4); pci->bars[i] = pci_read_config_long(cmd); } @@ -122,53 +110,44 @@ void scan_pci_bus(int bus) } } -pci_device_t *pci_find_device(unsigned int vendor, unsigned int device) -{ +pci_device_t *pci_find_device(unsigned int vendor, unsigned int device) { int i; list_head_t *p; pci_device_t *pci = 0; - list_for_each(p, &pci_devs) - { + list_for_each(p, &pci_devs) { pci = list_entry(p, pci_device_t, list); - if (pci->vendor == vendor && pci->device == device) - return pci; + if (pci->vendor == vendor && pci->device == device) return pci; } return 0; } -pci_device_t *pci_find_device_by_classcode(unsigned int classcode) -{ +pci_device_t *pci_find_device_by_classcode(unsigned int classcode) { int i; list_head_t *p; pci_device_t *pci = 0; - list_for_each(p, &pci_devs) - { + list_for_each(p, &pci_devs) { pci = list_entry(p, pci_device_t, list); - if (pci->classcode == classcode) - return pci; + if (pci->classcode == classcode) return pci; } return 0; } -void dump_pci_dev() -{ +void dump_pci_dev() { list_head_t *p; int i; - list_for_each(p, &pci_devs) - { + list_for_each(p, &pci_devs) { pci_device_t *pci = list_entry(p, pci_device_t, list); 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"); break; @@ -185,8 +164,7 @@ void dump_pci_dev() } } -int probe_pci_bus() -{ +int probe_pci_bus() { int v; int cmd; int dev, devfn; @@ -195,26 +173,21 @@ int probe_pci_bus() cmd = PCI_CMD(0, 0, 0, 0); v = pci_read_config_long(cmd); - if (v == 0xFFFFFFFF || v == 0x00000000 || v == 0x0000FFFF || v == 0xFFFF0000) - goto err; + 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 (devfn = 0; devfn < 8; devfn++) - { + for (dev = 0; dev < 32; dev++) { + 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) - return 1; + 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) - return 1; + if (v == PCI_VENDORID_INTEL || v == PCI_VENDORID_COMPAQ) return 1; } } @@ -225,188 +198,178 @@ err: return 0; } -void setup_pci() -{ - if (!probe_pci_bus()) - return; +void setup_pci() { + 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; } 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}}; - -const char *pci_get_info(unsigned int classcode, unsigned int progif) -{ +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}}; + +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->flag == 0) - { + if (p->code == code) { + if (p->flag == 0) { return p->info; - } - else - { + } else { s = p->info; } } diff --git a/kernel/printk.c b/kernel/printk.c index 6a1af0c..4094972 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -1,17 +1,17 @@ /* *-------------------------------------------------------------------------- * File Name: printk.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 Wed Jul 30 14:25:23 2008 * Version: 1.1 Tue Feb 10 22:40:25 2009 * Version: 2.0 Sun Mar 08 10:52:51 2009 * Version: 3.0 Sat Jul 18 23:06:27 2009 - * + * *-------------------------------------------------------------------------- */ @@ -21,23 +21,20 @@ extern void vga_dbg_puts(unsigned int line, unsigned int offset, const char *buf unsigned int printk_screen_nr = 0; extern unsigned int vga_screen_cnt(); -void switch_printk_screen() -{ +void switch_printk_screen() { printk_screen_nr++; printk_screen_nr %= vga_screen_cnt(); } char pkbuf[1024]; -int printk(const char *fmtstr, ...) -{ +int printk(const char *fmtstr, ...) { char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(pkbuf, fmtstr, args); vga_puts(printk_screen_nr, pkbuf, 0x2); return 0; } -int printd(const char *fmtstr, ...) -{ +int printd(const char *fmtstr, ...) { char *pdbuf = (char *)kmalloc(1024, 0); char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(pdbuf, fmtstr, args); @@ -47,8 +44,7 @@ int printd(const char *fmtstr, ...) } char plobuf[1024]; -int printlo(unsigned int line, unsigned int offset, const char *fmtstr, ...) -{ +int printlo(unsigned int line, unsigned int offset, const char *fmtstr, ...) { char *args = (char *)(((char *)&fmtstr) + 4); vsprintf(plobuf, fmtstr, args); vga_dbg_puts(line, offset, plobuf); diff --git a/kernel/sched.c b/kernel/sched.c index 6dde76d..a29a0a9 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -1,29 +1,29 @@ /* *-------------------------------------------------------------------------- * File Name: sched.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Tue Feb 10 11:53:21 2009 * Last Update: Tue Feb 10 11:53:21 2009 - * + * *-------------------------------------------------------------------------- */ #include "sched.h" + #include "assert.h" -#include "mm.h" #include "init.h" +#include "mm.h" #include "msr.h" task_union root_task __attribute__((__aligned__(PAGE_SIZE))); -pid_t get_next_pid() -{ +pid_t get_next_pid() { static pid_t g_pid = ROOT_TSK_PID; pid_t pid = ++g_pid; @@ -31,17 +31,13 @@ pid_t get_next_pid() return pid; } -void load_cr3(task_union *tsk) -{ - LOAD_CR3(tsk->cr3); -} +void load_cr3(task_union *tsk) { LOAD_CR3(tsk->cr3); } extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); list_head_t all_tasks; -void init_root_tsk() -{ +void init_root_tsk() { int i; // never use memset to init root_task @@ -60,7 +56,7 @@ void init_root_tsk() list_add(&root_task.list, &all_tasks); // TODO - //for(i=0; icr3); tss.esp0 = current->esp0; wrmsr(MSR_SYSENTER_ESP, current->esp0, 0); } -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;" @@ -115,37 +100,27 @@ void context_switch(task_union *prev, task_union *next) "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) + : [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 *find_task(pid_t pid) { task_union *p = 0; list_head_t *pos = 0, *tmp = 0; unsigned long iflags; irq_save(iflags); - list_for_each_safe(pos, tmp, &all_tasks) - { + list_for_each_safe(pos, tmp, &all_tasks) { p = list_entry(pos, task_union, list); - if (p->pid == pid) - break; + if (p->pid == pid) break; } irq_restore(iflags); return p; } -static const char *task_state(unsigned int state) -{ +static const char *task_state(unsigned int state) { static const char s[][16] = { " ERROR", "RUNNING", @@ -153,14 +128,12 @@ static const char *task_state(unsigned int state) "EXITING", }; - if (state >= TASK_END) - state = TASK_UNUSED; + if (state >= TASK_END) state = TASK_UNUSED; return s[state]; } -unsigned long schedule() -{ +unsigned long schedule() { static turn = 0; task_union *sel = &root_task; task_union *p = 0; @@ -173,49 +146,40 @@ unsigned long schedule() float min_ratio = 1.0; bool need_reset_weight = true; - list_for_each_safe(pos, t, &all_tasks) - { + list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_union, list); - if (p->state != TASK_RUNNING) - { + if (p->state != TASK_RUNNING) { continue; } - if (p->weight < p->priority) - { + if (p->weight < p->priority) { need_reset_weight = false; break; } } - if (need_reset_weight) - { - list_for_each_safe(pos, t, &all_tasks) - { + if (need_reset_weight) { + list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_union, list); - if (p->state != TASK_RUNNING) - { + if (p->state != TASK_RUNNING) { continue; } p->weight = 0; } } - list_for_each_safe(pos, t, &all_tasks) - { + list_for_each_safe(pos, t, &all_tasks) { 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); - if (p->state != TASK_RUNNING) - { + if (p->state != TASK_RUNNING) { continue; } - //printd("%08x %s weight %d\n", p, p->name, p->weight); + // printd("%08x %s weight %d\n", p, p->name, p->weight); float ratio = (float)(p->weight * 1.0) / (p->priority * 1.0); - if (ratio < min_ratio) - { + if (ratio < min_ratio) { sel = p; min_ratio = ratio; } @@ -228,14 +192,12 @@ unsigned long schedule() task_union *prev = current; task_union *next = sel; - if (prev != next) - { + if (prev != next) { context_switch(prev, next); } } -void debug_sched() -{ +void debug_sched() { task_union *p = list_entry(current->list.next, task_union, list); p->state = (p->state == TASK_RUNNING) ? TASK_WAIT : TASK_RUNNING; } diff --git a/kernel/semaphore.c b/kernel/semaphore.c index f817464..3e0fb69 100644 --- a/kernel/semaphore.c +++ b/kernel/semaphore.c @@ -6,33 +6,26 @@ * Description: none * ------------------------------------------------------------------------ */ -#include #include +#include -typedef struct semaphore_waiter -{ +typedef struct semaphore_waiter { list_head_t list; task_union *task; int up; } semaphore_waiter_t; #define SEMAPHORE_WAITER_INITIALIZER(name, task) \ - { \ - .list = LIST_HEAD_INIT((name).list), \ - .task = task, \ - .up = 0 \ - } + { .list = LIST_HEAD_INIT((name).list), .task = task, .up = 0 } -#define DECLARE_SEMAPHORE_WAITER(name, task) \ - semaphore_waiter_t name = SEMAPHORE_WAITER_INITIALIZER(name, task) +#define DECLARE_SEMAPHORE_WAITER(name, task) semaphore_waiter_t name = SEMAPHORE_WAITER_INITIALIZER(name, task) -void __down(semaphore_t *s) -{ +void __down(semaphore_t *s) { task_union *task = current; DECLARE_SEMAPHORE_WAITER(waiter, task); list_add_tail(&waiter.list, &s->wait_list); - //while(true) + // while(true) { task->state = TASK_WAIT; @@ -41,30 +34,25 @@ void __down(semaphore_t *s) disable_irq(); if (waiter.up) - ; //break; + ; // break; } } -void down(semaphore_t *s) -{ +void down(semaphore_t *s) { unsigned long iflags; irq_save(iflags); - if (likely(s->cnt > 0)) - { + if (likely(s->cnt > 0)) { s->cnt--; - } - else - { + } else { __down(s); } irq_restore(iflags); } -void __up(semaphore_t *s) -{ +void __up(semaphore_t *s) { semaphore_waiter_t *waiter = list_first_entry(&s->wait_list, semaphore_waiter_t, list); list_del(&waiter->list); waiter->up = 1; @@ -72,17 +60,13 @@ void __up(semaphore_t *s) waiter->task->state = TASK_RUNNING; } -void up(semaphore_t *s) -{ +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++; - } - else - { + } else { __up(s); } diff --git a/kernel/setup.c b/kernel/setup.c index 562feba..3add536 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -1,22 +1,22 @@ /* *-------------------------------------------------------------------------- * File Name: setup.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Mar 4 20:34:36 2009 * Last Update: Wed Mar 4 20:34:36 2009 - * + * *-------------------------------------------------------------------------- */ #include +#include #include #include -#include extern void setup_gdt(); extern void setup_idt(); @@ -38,24 +38,21 @@ extern void vga_init(); #define CLOCK_TICK_RATE 1193180 #define LATCH ((CLOCK_TICK_RATE + HZ / 2) / HZ) -void setup_i8253() -{ +void setup_i8253() { outb_p(0x34, 0x43); outb_p(LATCH & 0xFF, 0x40); outb(LATCH >> 8, 0x40); } #define VERSION "0.3.1" -const char *version = - "Kernel version " VERSION - " @ " BUILDER - " ["__DATE__ - " " __TIME__ "]" +const char *version = "Kernel version " VERSION " @ " BUILDER + " ["__DATE__ + " " __TIME__ + "]" - "\n"; + "\n"; -void setup_kernel() -{ +void setup_kernel() { extern char kernel_begin, kernel_end; vga_init(); @@ -86,9 +83,9 @@ void setup_kernel() setup_irqs(); return; - //switch_printk_screen(); + // switch_printk_screen(); setup_pci(); - //switch_printk_screen(); + // switch_printk_screen(); system_delay(); void ide_init(); ide_init(); @@ -99,8 +96,8 @@ void setup_kernel() setup_fs(); - //vga_puts(0, version, 0x2F); + // vga_puts(0, version, 0x2F); printk(version); - //switch_printk_screen(); + // switch_printk_screen(); } diff --git a/kernel/system.c b/kernel/system.c index c10c86f..9a82df8 100644 --- a/kernel/system.c +++ b/kernel/system.c @@ -1,38 +1,36 @@ /* *-------------------------------------------------------------------------- * File Name: system.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Mar 4 21:34:47 2009 * Last Update: Wed Mar 4 21:34:47 2009 - * + * *-------------------------------------------------------------------------- */ -#include -#include -#include -#include -#include -#include #include -#include #include +#include #include +#include +#include +#include +#include +#include #include +#include -void setup_gdt() -{ +void setup_gdt() { pDesc pdesc; - //change to new gdt. + // change to new gdt. sgdt(); - memcpy(gdt, (void *)pa2va(*((unsigned long *)(gdtr + 2))), - *((unsigned short *)gdtr)); + memcpy(gdt, (void *)pa2va(*((unsigned long *)(gdtr + 2))), *((unsigned short *)gdtr)); *((unsigned short *)gdtr) = NGDT * sizeof(Desc); *((unsigned long *)(gdtr + 2)) = (unsigned long)gdt; lgdt(); @@ -44,15 +42,13 @@ void setup_gdt() pdesc->seg.DPL = 3; } -void setup_idt() -{ +void setup_idt() { *((unsigned short *)idtr) = NIDT * sizeof(Gate); *((unsigned long *)(idtr + 2)) = (unsigned long)idt; lidt(); } -void setup_gate() -{ +void setup_gate() { int i; set_sys_int(0x00, TRAP_GATE, PRIVILEGE_KRNL, DivideError); set_sys_int(0x01, TRAP_GATE, PRIVILEGE_KRNL, Debug); @@ -71,11 +67,9 @@ void setup_gate() set_sys_int(0x0E, TRAP_GATE, PRIVILEGE_KRNL, PageFault); set_sys_int(0x10, TRAP_GATE, PRIVILEGE_KRNL, CoprocError); - for (i = 0x11; i < 0x20; i++) - set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); + for (i = 0x11; i < 0x20; i++) set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); - for (i = 0x20; i < 256; i++) - set_sys_int(i, INTR_GATE, PRIVILEGE_KRNL, no_irq_handler); + 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); @@ -96,29 +90,22 @@ void setup_gate() } void ide_irq(); -void default_ide_irq_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) -{ - //printk("default irq handler %d \n", irq); +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) -{ - printk("default irq handler %d \n", irq); -} +void default_irq_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { printk("default irq handler %d \n", irq); } -void setup_irqs() -{ +void setup_irqs() { extern void init_i8259(); init_i8259(); int i; - for (i = 0; i < NR_IRQS; i++) - { + for (i = 0; i < NR_IRQS; i++) { irq_desc[i] = no_irq_desc; - if (i < 16) - irq_desc[i].chip = &i8259_chip; + if (i < 16) irq_desc[i].chip = &i8259_chip; } void kbd_handler(unsigned int irq, pt_regs_t *regs, void *dev_id); @@ -128,22 +115,18 @@ void setup_irqs() request_irq(0x01, kbd_handler, "Intel 8042", "PS/2 Keyboard"); request_irq(0x0A, default_ide_irq_handler, "hard", "IDE"); request_irq(0x0E, default_ide_irq_handler, "hard", "IDE"); - for (i = 0; i < 16; i++) - { - if (i != 0 && i != 1 && i != 10 && i != 14) - request_irq(i, default_irq_handler, "default", "default"); + for (i = 0; i < 16; i++) { + if (i != 0 && i != 1 && i != 10 && i != 14) request_irq(i, default_irq_handler, "default", "default"); } - for (i = 0; i < 16; i++) - open_irq(i); + for (i = 0; i < 16; i++) open_irq(i); enable_irq(); } -void set_tss() -{ +void set_tss() { pTSS p = &tss; memset((void *)p, sizeof(TSS), 0); - p->esp0 = 0; // delay to init root_task + p->esp0 = 0; // delay to init root_task p->ss0 = SELECTOR_KRNL_DS; p->ss = SELECTOR_KRNL_DS; p->gs = SELECTOR_KRNL_DS; @@ -157,14 +140,11 @@ void set_tss() asm("ltr %%ax" ::"a"((INDEX_TSS << 3) + 3)); } -int sysc_reboot(int mode) -{ - +int sysc_reboot(int mode) { void do_reboot(); void do_poweroff(); - switch (mode) - { + switch (mode) { case 0: do_reboot(); break; @@ -176,16 +156,13 @@ int sysc_reboot(int mode) return 0; } -void system_delay() -{ +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 %%cr0, %%eax;" : "=a"(cr0)); asm("movl %%eax, %%cr0;" ::"a"(cr0)); } irq_restore(flags); diff --git a/kernel/wait.c b/kernel/wait.c index 9cf47b5..0d94545 100644 --- a/kernel/wait.c +++ b/kernel/wait.c @@ -1,46 +1,37 @@ /* *-------------------------------------------------------------------------- * File Name: wait.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Mon Feb 22 20:45:22 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include -void init_wait_queue(wait_queue_head_t *wqh) -{ - INIT_LIST_HEAD(&wqh->task_list); -} +void init_wait_queue(wait_queue_head_t *wqh) { INIT_LIST_HEAD(&wqh->task_list); } -void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) -{ +void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) { unsigned long flags; irq_save(flags); list_add_tail(&wq->task_list, &wqh->task_list); irq_restore(flags); } -void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) -{ +void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) { unsigned long flags; irq_save(flags); list_del(&wq->task_list); irq_restore(flags); } -void wake_up(wait_queue_head_t *wqh) -{ +void wake_up(wait_queue_head_t *wqh) { unsigned long flags; wait_queue_t *p, *tmp; irq_save(flags); - list_for_each_entry_safe(p, tmp, &wqh->task_list, task_list) - { - p->task->state = TASK_RUNNING; - } + list_for_each_entry_safe(p, tmp, &wqh->task_list, task_list) { p->task->state = TASK_RUNNING; } irq_restore(flags); // no schedule() here. @@ -49,26 +40,22 @@ void wake_up(wait_queue_head_t *wqh) #include DECLARE_WAIT_QUEUE_HEAD(debug_wq); unsigned int debug_global_var = 0; -int debug_wait_queue_get() -{ +int debug_wait_queue_get() { unsigned int v = 0; 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) - debug_global_var--; + if (debug_global_var != 0) debug_global_var--; enable_irq(); - if (v != 0) - break; + if (v != 0) break; schedule(); printd("pid %d is running\n", sysc_getpid()); @@ -81,36 +68,31 @@ int debug_wait_queue_get() return v; } -int debug_wait_queue_put(unsigned int v) -{ +int debug_wait_queue_put(unsigned int v) { debug_global_var = v; wake_up(&debug_wq); } -int sysc_wait(unsigned long pid) -{ +int sysc_wait(unsigned long pid) { task_union *p = find_task(pid); - if (p == 0) - return 0; + if (p == 0) return 0; - if (p->state == TASK_EXITING) - return 0; + if (p->state == TASK_EXITING) return 0; task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); add_wait_queue(&p->wait, &wait); - while (true) - { - //task->state = TASK_WAIT; + 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/errno.c b/lib/errno.c index 01b46b4..e4e0cf9 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -1,12 +1,12 @@ /* *-------------------------------------------------------------------------- * File Name: errno.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 01:49:49 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ int errno; diff --git a/lib/exec.c b/lib/exec.c index 208352c..a8bf906 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -1,17 +1,14 @@ /* *-------------------------------------------------------------------------- * File Name: exec.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 20:47:11 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include -int execv(const char *path, char *const argv[]) -{ - return syscall2(SYSC_EXEC, path, argv); -} +int execv(const char *path, char *const argv[]) { return syscall2(SYSC_EXEC, path, argv); } diff --git a/lib/exit.c b/lib/exit.c index 9887dba..ee19297 100644 --- a/lib/exit.c +++ b/lib/exit.c @@ -1,17 +1,14 @@ /* *-------------------------------------------------------------------------- * File Name: exit.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Thu Mar 4 10:11:57 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include -int exit(int status) -{ - syscall1(SYSC_EXIT, status); -} +int exit(int status) { syscall1(SYSC_EXIT, status); } diff --git a/lib/fork.c b/lib/fork.c index 2e45f23..0fa15dd 100644 --- a/lib/fork.c +++ b/lib/fork.c @@ -1,20 +1,19 @@ /* *-------------------------------------------------------------------------- * File Name: fork.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sun Feb 7 13:30:24 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include -#include #include -pid_t fork() -{ +#include +#include +pid_t fork() { #if 0 pid_t pid; //asm("xchg %bx, %bx;"); diff --git a/lib/keyboard.c b/lib/keyboard.c index 81b3eea..cbd4a74 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -1,23 +1,23 @@ /* *-------------------------------------------------------------------------- * File Name: keyboard.h - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Thu Jul 16 18:39:57 2009 * Last Update: Thu Jul 16 18:39:57 2009 - * + * *-------------------------------------------------------------------------- */ #if 0 -#include -#include -#include #include +#include +#include +#include #define EXT_KEY 0x80000000 /* None Print Key */ #define L_SHIFT_DOWN 0x00000100 #define R_SHIFT_DOWN 0x00000200 diff --git a/lib/lib.c b/lib/lib.c index afda33f..6c1fec6 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1,27 +1,22 @@ /* *-------------------------------------------------------------------------- * File Name: lib.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Wed Feb 17 18:58:13 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include #include -int isdigit(char c) -{ - return ('0' <= c && c <= '9'); -} +int isdigit(char c) { return ('0' <= c && c <= '9'); } -int atoi(const char *s) -{ +int atoi(const char *s) { int i = 0; - while (isdigit(*s)) - { + while (isdigit(*s)) { i *= 10; i += (*s++ - '0'); } @@ -29,27 +24,12 @@ int atoi(const char *s) return i; } -void reboot() -{ - syscall1(SYSC_REBOOT, 0); -} +void reboot() { syscall1(SYSC_REBOOT, 0); } -void poweroff() -{ - syscall1(SYSC_REBOOT, 1); -} +void poweroff() { syscall1(SYSC_REBOOT, 1); } -int systest() -{ - return syscall0(SYSC_TEST); -} +int systest() { return syscall0(SYSC_TEST); } -int sysdebug(unsigned int v) -{ - return syscall1(SYSC_DEBUG, v); -} +int sysdebug(unsigned int v) { return syscall1(SYSC_DEBUG, v); } -int pause(unsigned long tick) -{ - return syscall1(SYSC_PAUSE, tick); -} +int pause(unsigned long tick) { return syscall1(SYSC_PAUSE, tick); } diff --git a/lib/open.c b/lib/open.c index 9b5f3a3..c4072fb 100644 --- a/lib/open.c +++ b/lib/open.c @@ -1,17 +1,16 @@ /* *-------------------------------------------------------------------------- * File Name: open.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 01:15:29 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ #include -int open(const char *path, int flags, ...) -{ +int open(const char *path, int flags, ...) { // 不支持第三个参数 return syscall3(SYSC_OPEN, path, flags, 0); } diff --git a/lib/read.c b/lib/read.c index 35326ae..6376610 100644 --- a/lib/read.c +++ b/lib/read.c @@ -1,17 +1,14 @@ /* *-------------------------------------------------------------------------- * File Name: read.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 18:54:48 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include #include -ssize_t read(int fd, void *buf, size_t count) -{ - return (ssize_t)syscall3(SYSC_READ, fd, buf, count); -} +#include +ssize_t read(int fd, void *buf, size_t count) { return (ssize_t)syscall3(SYSC_READ, fd, buf, count); } diff --git a/lib/stat.c b/lib/stat.c index bb99610..7e5fa32 100644 --- a/lib/stat.c +++ b/lib/stat.c @@ -1,23 +1,17 @@ /* *-------------------------------------------------------------------------- * File Name: stat.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Tue Feb 23 19:27:15 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include -#include #include +#include +#include -int _stat(int fd, struct stat *stat) -{ - return syscall2(SYSC_STAT, fd, stat); -} -int fstat(int fd, struct stat *buf) -{ - return _stat(fd, buf); -} +int _stat(int fd, struct stat *stat) { return syscall2(SYSC_STAT, fd, stat); } +int fstat(int fd, struct stat *buf) { return _stat(fd, buf); } diff --git a/lib/string.c b/lib/string.c index a7d0daa..af0b26d 100644 --- a/lib/string.c +++ b/lib/string.c @@ -5,49 +5,39 @@ #include "string.h" -char *strcpy(char *dest, const char *src) -{ +char *strcpy(char *dest, const char *src) { char *p = dest; while ((*dest++ = *src++)) ; return p; } -size_t strlen(const char *str) -{ +size_t strlen(const char *str) { int i = 0; - while (*str++) - i++; + while (*str++) i++; return i; } -int strcmp(const char *a, const char *b) -{ +int strcmp(const char *a, const char *b) { int delta; - while (*a || *b) - { + while (*a || *b) { delta = *a++ - *b++; - if (delta != 0) - return delta; + if (delta != 0) return delta; } return 0; } -int strncmp(const char *a, const char *b, size_t count) -{ +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) - return delta; + if (delta != 0) return delta; - if (c1 == 0) - break; + if (c1 == 0) break; count--; } @@ -55,21 +45,17 @@ int strncmp(const char *a, const char *b, size_t count) return 0; } -char *strcat(char *dest, const char *src) -{ +char *strcat(char *dest, const char *src) { char *tmp = dest; - while (*dest) - dest++; + while (*dest) dest++; while ((*dest++ = *src++) != '\0') ; return tmp; } -void *memcpy(void *dest, const void *src, size_t size) -{ +void *memcpy(void *dest, const void *src, size_t size) { char *d = (char *)dest; char *s = (char *)src; - while (size-- > 0) - { + while (size-- > 0) { *d = *s; d++; s++; @@ -77,35 +63,27 @@ void *memcpy(void *dest, const void *src, size_t size) return dest; } -void memset(void *dest, char ch, size_t size) -{ +void memset(void *dest, char ch, size_t size) { char *d = (char *)dest; - while (size--) - *d++ = ch; + while (size--) *d++ = ch; } -int memcmp(const void *a, const void *b, size_t count) -{ +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) - break; + if ((delta = *sa - *sb) != 0) break; return delta; } -char *strstr(const char *a, const char *b) -{ +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) - return (char *)a; + if (memcmp(a, b, lb) == 0) return (char *)a; a++; } diff --git a/lib/syscall.c b/lib/syscall.c index 1f86e73..ed67945 100644 --- a/lib/syscall.c +++ b/lib/syscall.c @@ -18,72 +18,42 @@ "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) -{ +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) -{ +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) -{ +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) -{ +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__; } -int _syscall0(int nr) -{ - return __syscall0(nr); -} +int _syscall0(int nr) { return __syscall0(nr); } -int _syscall1(int nr, unsigned long a) -{ - return __syscall1(nr, a); -} +int _syscall1(int nr, unsigned long a) { return __syscall1(nr, a); } -int _syscall2(int nr, unsigned long a, unsigned long b) -{ - return __syscall2(nr, a, b); -} +int _syscall2(int nr, unsigned long a, unsigned long b) { return __syscall2(nr, a, b); } -int _syscall3(int nr, unsigned long a, unsigned long b, unsigned long c) -{ - return __syscall3(nr, a, b, c); -} +int _syscall3(int nr, unsigned long a, unsigned long b, unsigned long c) { return __syscall3(nr, a, b, c); } -int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d) -{ - return __syscall4(nr, a, b, c, d); -} +int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d) { return __syscall4(nr, a, b, c, d); } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 870829c..1152327 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -10,16 +10,10 @@ char *itoa(char *s, int n); char *itou(char *s, unsigned int n); char *itox(char *s, unsigned int n); -enum -{ - ALIGN_RIGHT, - ALIGN_LEFT -}; +enum { ALIGN_RIGHT, ALIGN_LEFT }; -int write_buf(char *buf, const char *str, char fillch, int charcnt, int align) -{ - if (str == 0) - return 0; +int write_buf(char *buf, const char *str, char fillch, int charcnt, int align) { + if (str == 0) return 0; int len = strlen(str); int delta_char_cnt = charcnt - len; @@ -27,8 +21,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; } @@ -36,24 +29,20 @@ 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 < delta_char_cnt; ++i) - { + for (i = 0; i < delta_char_cnt; ++i) { buf[c_pos + i] = fillch; } return charcnt > len ? charcnt : len; } -int vsprintf(char *buf, const char *fmt, char *args) -{ +int vsprintf(char *buf, const char *fmt, char *args) { char *p = buf; int char_cnt; char tmp[64]; - while (*fmt) - { - if (*fmt != '%') - { + while (*fmt) { + if (*fmt != '%') { *p++ = *fmt++; continue; } @@ -61,30 +50,26 @@ int vsprintf(char *buf, const char *fmt, char *args) fmt++; int align = ALIGN_RIGHT; - if (*(fmt) == '-') - { + if (*(fmt) == '-') { align = ALIGN_LEFT; ++fmt; } char char_fill = ' '; - if (*(fmt) == '0' || *(fmt) == ' ') - { + if (*(fmt) == '0' || *(fmt) == ' ') { char_fill = *(fmt); ++fmt; } char_cnt = 0; - while (*(fmt) >= '0' && *(fmt) <= '9') - { + while (*(fmt) >= '0' && *(fmt) <= '9') { char_cnt += *(fmt) - '0'; char_cnt *= 10; ++fmt; } char_cnt /= 10; - switch (*fmt) - { + switch (*fmt) { case 'c': *p++ = *args; break; @@ -112,94 +97,78 @@ int vsprintf(char *buf, const char *fmt, char *args) *p = 0; } -void swap_char(char *a, char *b) -{ +void swap_char(char *a, char *b) { char c; c = *a; *a = *b; *b = c; } -char *itoa(char *s, int n) -{ +char *itoa(char *s, int n) { int i = 0; char *p = 0; - if (n & 0x80000000) - { + if (n & 0x80000000) { n = ~n + 1; *s++ = '-'; } p = s; - do - { + do { *p++ = (n % 10) + '0'; n /= 10; } while (n); *p-- = 0; - while (s < p) - { + while (s < p) { swap_char(s, p); s++; p--; } } -char *itou(char *s, unsigned int n) -{ +char *itou(char *s, unsigned int n) { char c; char *p = s; - do - { + do { *p++ = (n % 10) + '0'; n /= 10; } while (n); *p-- = 0; - while (s < p) - { + while (s < p) { swap_char(s, p); s++; p--; } } -char *itox(char *s, unsigned int n) -{ +char *itox(char *s, unsigned int n) { char *p = s; char ch; int i; bool flag = false; - for (i = 28; i >= 0; i -= 4) - { + for (i = 28; i >= 0; i -= 4) { ch = (n >> i) & 0x0F; - if (ch >= 0 && ch <= 9) - { + if (ch >= 0 && ch <= 9) { ch += '0'; - } - else - { + } else { ch -= 10; ch += 'A'; } - if (ch != '0') - flag = true; + if (ch != '0') flag = true; - if (flag || ch != '0') - *p++ = ch; + if (flag || ch != '0') *p++ = ch; } - if (s == p) - *p++ = '0'; + if (s == p) *p++ = '0'; *p = 0; diff --git a/lib/wait.c b/lib/wait.c index 8a9294d..2b902b7 100644 --- a/lib/wait.c +++ b/lib/wait.c @@ -8,7 +8,4 @@ */ #include -int wait(unsigned long pid) -{ - syscall1(SYSC_WAIT, pid); -} +int wait(unsigned long pid) { syscall1(SYSC_WAIT, pid); } diff --git a/lib/write.c b/lib/write.c index 09d2b27..04353c6 100644 --- a/lib/write.c +++ b/lib/write.c @@ -1,27 +1,26 @@ /* *-------------------------------------------------------------------------- * File Name: write.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Mon Mar 9 02:00:09 2009 * Last Update: Mon Mar 9 02:00:09 2009 - * + * *-------------------------------------------------------------------------- */ #include -int write(int fd, const char *buf, unsigned long size) -{ - //asm(""::"c"(size),"d"(buf),"b"(fd)); - //sysenter(0); - //syscall3(0, fd, buf, size); - //asm("nop;nop;nop;"); +int write(int fd, const char *buf, unsigned long size) { + // asm(""::"c"(size),"d"(buf),"b"(fd)); + // sysenter(0); + // syscall3(0, fd, buf, size); + // asm("nop;nop;nop;"); syscall3(SYSC_WRITE, fd, buf, size); diff --git a/mm/bootmem.c b/mm/bootmem.c index d332411..bd12199 100644 --- a/mm/bootmem.c +++ b/mm/bootmem.c @@ -6,169 +6,144 @@ * Description: none * ------------------------------------------------------------------------ */ +#include +#include #include #include -#include -#include extern char kernel_begin, kernel_end; -static void e820_print_type(unsigned long type) -{ - switch (type) - { - case E820_RAM: - printk("usable"); - break; - case E820_RESERVED: - printk("reserved"); - break; - case E820_ACPI: - printk("ACPI data"); - break; - case E820_NVS: - printk("ACPI NVS"); - break; - case E820_UNUSABLE: - printk("unusable"); - break; - default: - printk("type %x", type); - break; - } +static void e820_print_type(unsigned long type) { + switch (type) { + case E820_RAM: + printk("usable"); + break; + case E820_RESERVED: + printk("reserved"); + break; + case E820_ACPI: + printk("ACPI data"); + break; + case E820_NVS: + printk("ACPI NVS"); + break; + case E820_UNUSABLE: + printk("unusable"); + break; + default: + printk("type %x", type); + break; + } } -void e820_print_map() -{ - unsigned int i = 0; +void e820_print_map() { + unsigned int i = 0; - for (i = 0; i < boot_params.e820map.map_cnt; ++i) - { - struct e820_entry *p = boot_params.e820map.map + i; + for (i = 0; i < boot_params.e820map.map_cnt; ++i) { + struct e820_entry *p = boot_params.e820map.map + i; - 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); + 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); + e820_print_type(p->type); - printk("\n"); - } + printk("\n"); + } } bootmem_data_t bootmem_data; -unsigned long bootmem_max_pfn() -{ - return bootmem_data.max_pfn; -} +unsigned long bootmem_max_pfn() { return bootmem_data.max_pfn; } -unsigned long bootmem_page_state(unsigned long pfn) -{ - return constant_test_bit(pfn, bootmem_data.bitmap); -} +unsigned long bootmem_page_state(unsigned long pfn) { return constant_test_bit(pfn, bootmem_data.bitmap); } -void e820_init_bootmem_data() -{ - unsigned int i = 0; +void e820_init_bootmem_data() { + unsigned int i = 0; - memset(&bootmem_data, 0, sizeof(bootmem_data)); - bootmem_data.min_pfn = ~0UL; + memset(&bootmem_data, 0, sizeof(bootmem_data)); + bootmem_data.min_pfn = ~0UL; - unsigned long bgn_pfn; - unsigned long end_pfn; + unsigned long bgn_pfn; + unsigned long end_pfn; - for (i = 0; i < boot_params.e820map.map_cnt; ++i) - { - struct e820_entry *p = boot_params.e820map.map + i; + for (i = 0; i < boot_params.e820map.map_cnt; ++i) { + struct e820_entry *p = boot_params.e820map.map + i; - if (p->type != E820_RAM) - continue; + if (p->type != E820_RAM) continue; - bgn_pfn = PFN_UP(p->addr); - end_pfn = PFN_DW(p->addr + p->size); + bgn_pfn = PFN_UP(p->addr); + end_pfn = PFN_DW(p->addr + p->size); - if (bootmem_data.max_pfn < end_pfn) - bootmem_data.max_pfn = end_pfn; - } + if (bootmem_data.max_pfn < end_pfn) bootmem_data.max_pfn = end_pfn; + } - bootmem_data.min_pfn = 0; + bootmem_data.min_pfn = 0; - // limit max_pfn - unsigned long max_support_pfn = PFN_DW(MAX_SUPT_PHYMM_SIZE); - if (bootmem_data.max_pfn > max_support_pfn) - { - bootmem_data.max_pfn = max_support_pfn; - } + // limit max_pfn + unsigned long max_support_pfn = PFN_DW(MAX_SUPT_PHYMM_SIZE); + if (bootmem_data.max_pfn > max_support_pfn) { + bootmem_data.max_pfn = max_support_pfn; + } } -void register_bootmem_pages() -{ - unsigned int i = 0; - unsigned int j = 0; +void register_bootmem_pages() { + unsigned int i = 0; + unsigned int j = 0; - for (i = 0; i < boot_params.e820map.map_cnt; ++i) - { - struct e820_entry *p = boot_params.e820map.map + i; + for (i = 0; i < boot_params.e820map.map_cnt; ++i) { + struct e820_entry *p = boot_params.e820map.map + i; - if (p->type != E820_RAM) - continue; + if (p->type != E820_RAM) continue; - unsigned long bgn_pfn = PFN_UP(p->addr); - unsigned long end_pfn = PFN_DW(p->addr + p->size); + unsigned long bgn_pfn = PFN_UP(p->addr); + unsigned long end_pfn = PFN_DW(p->addr + p->size); - for (j = bgn_pfn; j < end_pfn; ++j) - { - test_and_clear_bit(j, bootmem_data.bitmap); + for (j = bgn_pfn; j < end_pfn; ++j) { + test_and_clear_bit(j, bootmem_data.bitmap); + } } - } } -void reserve_bootmem(unsigned long bgn_pfn, unsigned long end_pfn) -{ - //printk("reserve %d %d\n", bgn_pfn, end_pfn); +void reserve_bootmem(unsigned long bgn_pfn, unsigned long end_pfn) { + // printk("reserve %d %d\n", bgn_pfn, end_pfn); - int i = 0; - for (i = bgn_pfn; i < end_pfn; ++i) - { - test_and_set_bit(i, bootmem_data.bitmap); - } + int i = 0; + for (i = bgn_pfn; i < end_pfn; ++i) { + test_and_set_bit(i, bootmem_data.bitmap); + } } -void reserve_kernel_pages() -{ - //reserve_bootmem(PFN_DW(va2pa(&kernel_begin)), PFN_UP(va2pa(&kernel_end))); - reserve_bootmem(0, PFN_UP(va2pa(&kernel_end))); +void reserve_kernel_pages() { + // reserve_bootmem(PFN_DW(va2pa(&kernel_begin)), PFN_UP(va2pa(&kernel_end))); + reserve_bootmem(0, PFN_UP(va2pa(&kernel_end))); } -void reserve_bootmem_pages() -{ - unsigned long bgn_pfn = PFN_DW(va2pa(bootmem_data.bitmap)); +void reserve_bootmem_pages() { + unsigned long bgn_pfn = PFN_DW(va2pa(bootmem_data.bitmap)); - unsigned long end_pfn = bgn_pfn + PFN_UP(bootmem_data.mapsize); + unsigned long end_pfn = bgn_pfn + PFN_UP(bootmem_data.mapsize); - reserve_bootmem(bgn_pfn, end_pfn); + reserve_bootmem(bgn_pfn, end_pfn); } -void init_bootmem_allocator() -{ - int mapsize = (bootmem_data.max_pfn + 7) / 8; +void init_bootmem_allocator() { + int mapsize = (bootmem_data.max_pfn + 7) / 8; - bootmem_data.bitmap = &kernel_end; - bootmem_data.mapsize = mapsize; + bootmem_data.bitmap = &kernel_end; + bootmem_data.mapsize = mapsize; - memset(bootmem_data.bitmap, 0xFF, mapsize); + memset(bootmem_data.bitmap, 0xFF, mapsize); - register_bootmem_pages(); + register_bootmem_pages(); - reserve_kernel_pages(); + reserve_kernel_pages(); - reserve_bootmem_pages(); + reserve_bootmem_pages(); } -void init_bootmem() -{ - e820_print_map(); - e820_init_bootmem_data(); - init_bootmem_allocator(); +void init_bootmem() { + e820_print_map(); + e820_init_bootmem_data(); + init_bootmem_allocator(); #if 0 printk("alloc 10 bytes align 8 addr %08x\n", alloc_bootmem(10, 8)); @@ -180,85 +155,77 @@ void init_bootmem() #endif } -void *alloc_bootmem(unsigned long size, unsigned long align) -{ - bootmem_data_t *pbd = &bootmem_data; +void *alloc_bootmem(unsigned long size, unsigned long align) { + bootmem_data_t *pbd = &bootmem_data; - assert(size != 0); - assert((align & (align - 1)) == 0); // must be power of 2 + assert(size != 0); + assert((align & (align - 1)) == 0); // must be power of 2 - unsigned long fallback = 0; - unsigned long bgn_pfn, end_pfn, step; + unsigned long fallback = 0; + unsigned long bgn_pfn, end_pfn, step; - step = align >> PAGE_SHIFT; - step = step > 0 ? step : 1; + step = align >> PAGE_SHIFT; + step = step > 0 ? step : 1; - bgn_pfn = ALIGN(pbd->min_pfn, step); - end_pfn = pbd->max_pfn; + bgn_pfn = ALIGN(pbd->min_pfn, step); + end_pfn = pbd->max_pfn; - // start from last position - if (pbd->last_hit_pfn > bgn_pfn) - { - fallback = bgn_pfn + 1; - bgn_pfn = ALIGN(pbd->last_hit_pfn, step); - } + // start from last position + if (pbd->last_hit_pfn > bgn_pfn) { + fallback = bgn_pfn + 1; + bgn_pfn = ALIGN(pbd->last_hit_pfn, step); + } - while (1) - { - int merge; - void *region; - unsigned long i, search_end_pfn; - unsigned long start_off, end_off; + 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); + 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); + search_end_pfn = bgn_pfn + PFN_UP(size); - if (bgn_pfn >= end_pfn || search_end_pfn > end_pfn) - break; + if (bgn_pfn >= end_pfn || search_end_pfn > end_pfn) break; - for (i = bgn_pfn; i < search_end_pfn; ++i) - { - if (bootmem_page_state(i) != BOOTMEM_PAGE_FREE) - { // space not enough - bgn_pfn = ALIGN(i, step); - if (bgn_pfn == i) - bgn_pfn += step; + for (i = bgn_pfn; i < search_end_pfn; ++i) { + if (bootmem_page_state(i) != BOOTMEM_PAGE_FREE) { // space not enough + bgn_pfn = ALIGN(i, step); + if (bgn_pfn == i) bgn_pfn += step; - goto find_block; - } - } + goto find_block; + } + } - // try to use the unused part of last page - 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); + // try to use the unused part of last page + 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; - end_off = start_off + size; + merge = PFN_DW(start_off) < bgn_pfn; + end_off = start_off + size; - pbd->last_offset = end_off; - pbd->last_hit_pfn = PFN_UP(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)); + reserve_bootmem(PFN_DW(start_off) + merge, PFN_UP(end_off)); - region = pa2va(start_off); + region = pa2va(start_off); - memset(region, 0, size); + memset(region, 0, size); - return region; - } + return region; + } - if (fallback) - { - bgn_pfn = ALIGN(fallback - 1, step); - fallback = 0; - goto find_block; - } + if (fallback) { + bgn_pfn = ALIGN(fallback - 1, step); + fallback = 0; + goto find_block; + } - return 0; + return 0; } \ No newline at end of file diff --git a/mm/buddy.c b/mm/buddy.c index 7869f30..68c2a81 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -6,12 +6,11 @@ * Description: none * ------------------------------------------------------------------------ */ -#include #include +#include #include -struct buddy_system -{ +struct buddy_system { page_t *page_map; page_t *page_map_end; free_area_t free_area[MAX_ORDER]; @@ -19,22 +18,18 @@ struct buddy_system struct buddy_system buddy_system; -int buddy_is_free(page_t *page, unsigned int order) -{ - if (PagePrivate(page) && page->private == order) - return 1; +int buddy_is_free(page_t *page, unsigned int order) { + if (PagePrivate(page) && page->private == order) return 1; return 0; } -page_t *_va2page(unsigned long addr) -{ +page_t *_va2page(unsigned long addr) { page_t *page = buddy_system.page_map + va2pfn(addr); assert(page >= buddy_system.page_map); - //assert(page < buddy_system.page_map_end); - if (page >= buddy_system.page_map_end) - { + // assert(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); panic("error"); @@ -42,20 +37,15 @@ page_t *_va2page(unsigned long addr) return page; } -page_t *_pa2page(unsigned long paddr) -{ +page_t *_pa2page(unsigned long paddr) { unsigned long vaddr = (unsigned long)pa2va(paddr); - //printk("%s paddr %08x vaddr %08x\n", __func__, paddr, vaddr); + // 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); -} +void *page2va(page_t *page) { return pfn2va((page)-buddy_system.page_map); } -page_t *__alloc_pages(unsigned int order) -{ +page_t *__alloc_pages(unsigned int order) { // page_t *page = 0; page_t *buddy = 0; @@ -63,11 +53,9 @@ page_t *__alloc_pages(unsigned int order) unsigned long size; unsigned int select_order; unsigned int i; - for (select_order = order; select_order < MAX_ORDER; ++select_order) - { + for (select_order = order; select_order < MAX_ORDER; ++select_order) { area = buddy_system.free_area + select_order; - if (!list_empty(&(area->free_list))) - { + if (!list_empty(&(area->free_list))) { goto found; } } @@ -81,8 +69,7 @@ found: page->private = 0; area->free_count--; - while (select_order > order) - { + while (select_order > order) { area--; select_order--; size = 1UL << select_order; @@ -95,8 +82,7 @@ found: } // - for (i = 0; i < (1UL << order); ++i) - { + for (i = 0; i < (1UL << order); ++i) { page_t *p = page + i; p->head_page = page; p->order = order; @@ -107,8 +93,7 @@ found: return page; } -unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) -{ +unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) { // gfp_mask // ... @@ -121,20 +106,16 @@ unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) return addr; } -void __free_pages(page_t *page, unsigned int order) -{ - if (order > MAX_ORDER) - return; +void __free_pages(page_t *page, unsigned int order) { + 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)) - { + 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; } @@ -157,10 +138,8 @@ void __free_pages(page_t *page, unsigned int order) buddy_system.free_area[order].free_count++; } -void free_pages(unsigned long addr) -{ - if (!valid_va(addr)) - { +void free_pages(unsigned long addr) { + if (!valid_va(addr)) { BUG_ON(!valid_va(addr)); } @@ -172,20 +151,16 @@ void free_pages(unsigned long addr) irq_restore(flags); } -void dump_buddy_system() -{ +void dump_buddy_system() { unsigned long i; - for (i = 0; i < MAX_ORDER; ++i) - { + for (i = 0; i < MAX_ORDER; ++i) { printk("order %2d free_count %d ", i, buddy_system.free_area[i].free_count); - if (buddy_system.free_area[i].free_count < 100) - { + if (buddy_system.free_area[i].free_count < 100) { list_head_t *p; page_t *page; printk("pfn:"); - list_for_each(p, &buddy_system.free_area[i].free_list) - { + list_for_each(p, &buddy_system.free_area[i].free_list) { page = list_entry(p, page_t, lru); printk(" %d", page->index); } @@ -210,8 +185,7 @@ void dump_buddy_system() #endif } -void init_buddy_system() -{ +void init_buddy_system() { page_t *page; unsigned long i; unsigned long pfn_cnt = bootmem_max_pfn(); @@ -219,16 +193,14 @@ void init_buddy_system() // init free area memset(&buddy_system, 0, sizeof(buddy_system)); - for (i = 0; i < MAX_ORDER; ++i) - { + for (i = 0; i < MAX_ORDER; ++i) { INIT_LIST_HEAD(&(buddy_system.free_area[i].free_list)); } // init page map unsigned long page_map_size = pfn_cnt * sizeof(page_t); buddy_system.page_map = alloc_bootmem(page_map_size, PAGE_SIZE); - if (0 == buddy_system.page_map) - { + if (0 == buddy_system.page_map) { printk("can not go on playing...\n"); while (1) ; @@ -236,8 +208,7 @@ void init_buddy_system() buddy_system.page_map_end = buddy_system.page_map + pfn_cnt + 1; printk("page_map begin %08x end %08x pfncnt %u page_t size %u\n", buddy_system.page_map, buddy_system.page_map_end, pfn_cnt, sizeof(page_t)); - for (i = 0; i < pfn_cnt; ++i) - { + for (i = 0; i < pfn_cnt; ++i) { page = buddy_system.page_map + i; memset((void *)page, 0, sizeof(page_t)); page->private = 0; @@ -247,12 +218,10 @@ void init_buddy_system() } // get free pages from bootmem - for (i = 0; i < pfn_cnt; ++i) - { + for (i = 0; i < pfn_cnt; ++i) { page = buddy_system.page_map + i; - if (BOOTMEM_PAGE_FREE == bootmem_page_state(i)) - { + if (BOOTMEM_PAGE_FREE == bootmem_page_state(i)) { // free to buddy system __free_pages(page, 0); } @@ -261,5 +230,5 @@ void init_buddy_system() // free bootmem bitmap pages to buddy system // ... - //dump_buddy_system(); + // dump_buddy_system(); } diff --git a/mm/mm.c b/mm/mm.c index 2f5a181..ae3e7d5 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -1,26 +1,26 @@ /* *-------------------------------------------------------------------------- * File Name: mm.c - * + * * Description: none - * - * + * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] - * + * * Version: 1.0 * Create Date: Wed Mar 4 21:08:47 2009 * Last Update: Wed Mar 4 21:08:47 2009 - * + * *-------------------------------------------------------------------------- */ +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include extern char etext, edata, end; extern void init_buddy_system(); @@ -29,8 +29,7 @@ extern void init_slub_system(); 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) -{ +void set_page_shared(void *x) { unsigned long addr = (unsigned long)x; addr = PAGE_ALIGN(addr); unsigned int npd = get_npd(addr); @@ -42,8 +41,7 @@ void set_page_shared(void *x) extern void sysexit(); -void init_paging() -{ +void init_paging() { unsigned int i; unsigned long pfn = 0; pte_t *pte = 0; @@ -52,15 +50,12 @@ void init_paging() // 在multiboot.S是已经初始化了BOOT_INIT_PAGETBL_CNT个页 // 这里接着初始化剩余的页 // 最大限制内存1G - for (pfn = pa2pfn(BOOT_INIT_PAGETBL_CNT << 22); pfn < bootmem_data.max_pfn; ++pfn) - { + for (pfn = pa2pfn(BOOT_INIT_PAGETBL_CNT << 22); pfn < bootmem_data.max_pfn; ++pfn) { unsigned long ti = pfn % PAGE_PTE_CNT; unsigned long page_addr = pfn2pa(pfn); - if (ti == 0) - { + if (ti == 0) { pgtb_addr = (unsigned long)va2pa(bootmem_alloc_pages(1)); - if (0 == pgtb_addr) - panic("No Pages for Paging..."); + if (0 == pgtb_addr) panic("No Pages for Paging..."); memset((void *)pgtb_addr, 0, PAGE_SIZE); @@ -73,8 +68,7 @@ void init_paging() // paging for kernel space unsigned long delta = get_npd(PAGE_OFFSET); - for (i = delta; i < PDECNT_PER_PAGE; ++i) - { + for (i = delta; i < PDECNT_PER_PAGE; ++i) { init_pgd[i] = init_pgd[i - delta]; init_pgd[i - delta] = 0; } @@ -85,8 +79,7 @@ void init_paging() LOAD_CR3(init_pgd); } -void init_mm() -{ +void init_mm() { printk("init bootmem alloc...\n"); init_bootmem(); printk("init global paging...\n"); diff --git a/mm/page.c b/mm/page.c index 5b11826..31c14cb 100644 --- a/mm/page.c +++ b/mm/page.c @@ -1,24 +1,23 @@ /* *-------------------------------------------------------------------------- * File Name: page.c - * + * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sun Jan 24 15:14:24 2010 - * + * * Description: none - * + * *-------------------------------------------------------------------------- */ -#include -#include -#include #include -#include #include +#include +#include +#include +#include -void do_no_page(void *addr) -{ +void do_no_page(void *addr) { pde_t *page_dir = (pde_t *)current->cr3; pte_t *page_tbl = 0; @@ -28,8 +27,7 @@ void do_no_page(void *addr) int npde = get_npd(addr); int npte = get_npt(addr); - if (page_dir[npde] == 0) - { + if (page_dir[npde] == 0) { page_tbl = (pte_t *)alloc_one_page(0); assert(page_tbl != 0); @@ -44,11 +42,9 @@ void do_no_page(void *addr) load_cr3(current); } -void do_wp_page(void *addr) -{ - //printk("%s addr %08x current %08x\n", __func__, (unsigned long)addr, current); - if ((unsigned long)addr >= PAGE_OFFSET) - { +void do_wp_page(void *addr) { + // printk("%s addr %08x current %08x\n", __func__, (unsigned long)addr, current); + if ((unsigned long)addr >= PAGE_OFFSET) { panic("%s invalid addr", __func__); } @@ -61,8 +57,7 @@ void do_wp_page(void *addr) 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--; unsigned long flags = PAGE_FLAGS(page_tbl[npte]); unsigned long wp_va_addr = (unsigned long)pa2va(wp_pa_addr); diff --git a/mm/slub.c b/mm/slub.c index 6c0e4a0..2e48619 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -7,9 +7,9 @@ * ------------------------------------------------------------------------ */ +#include #include #include -#include list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); @@ -21,8 +21,7 @@ list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); static kmem_cache_t kmalloc_caches[SLUB_INIT_CACHE_SIZE]; -static bool calculate_params(kmem_cache_t *cache) -{ +static bool calculate_params(kmem_cache_t *cache) { // calculate size unsigned long size = cache->objsize; unsigned long align = cache->align; @@ -33,35 +32,26 @@ static bool calculate_params(kmem_cache_t *cache) // calculate order unsigned long order; - for (order = 1; order < MAX_ORDER; ++order) - { - if ((PAGE_SIZE << order) / cache->size >= 4) - { + for (order = 1; order < MAX_ORDER; ++order) { + 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) - return false; + if (0 == cache->objects) return false; return true; } -static bool kmem_cache_init(kmem_cache_t *cache, - const char *name, - size_t size, - size_t align) -{ - +static bool kmem_cache_init(kmem_cache_t *cache, const char *name, size_t size, size_t align) { memset(cache, 0, sizeof(kmem_cache_t)); cache->name = name; @@ -71,8 +61,7 @@ static bool kmem_cache_init(kmem_cache_t *cache, cache->partial_cnt = 0; INIT_LIST_HEAD(&(cache->partial)); - if (!calculate_params(cache)) - goto err; + if (!calculate_params(cache)) goto err; return true; err: @@ -80,10 +69,8 @@ err: return false; } -static page_t *get_partial(kmem_cache_t *cache, gfp_t gfpflags) -{ - if (list_empty(&cache->partial)) - return 0; +static page_t *get_partial(kmem_cache_t *cache, gfp_t gfpflags) { + if (list_empty(&cache->partial)) return 0; list_head_t *p = cache->partial.next; list_del(p); @@ -95,22 +82,19 @@ static page_t *get_partial(kmem_cache_t *cache, gfp_t gfpflags) return page; } -static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) -{ +static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) { // alloc pages from buddy system unsigned long bgn = alloc_pages(gfpflags, cache->order); unsigned long end = 0; page_t *page = va2page(bgn); - if (0 == page) - return 0; + if (0 == page) return 0; end = bgn + cache->objects * cache->size; unsigned long last = bgn; unsigned long addr; - for (addr = bgn; addr < end; addr += cache->size) - { + for (addr = bgn; addr < end; addr += cache->size) { *((void **)last) = (void *)addr; last = addr; } @@ -124,39 +108,29 @@ static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) return page; } -static void *__slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) -{ +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; } - } - else - { + } else { cache->page = page; } } - if (cache->page == 0) - return 0; + if (cache->page == 0) return 0; object = cache->page->freelist; - if (object == 0) - { + if (object == 0) { cache->page = 0; - } - else - { + } else { cache->page->freelist = object[0]; cache->page->inuse++; } @@ -164,23 +138,18 @@ static void *__slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) return object; } -static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) -{ +static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) { void **object = 0; - if (cache == 0) - return 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); - } - else - { + } else { object = cache->page->freelist; cache->page->freelist = object[0]; cache->page->inuse++; @@ -191,28 +160,24 @@ static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) return object; } -static void __slub_free(kmem_cache_t *cache, page_t *page, void *addr) -{ +static void __slub_free(kmem_cache_t *cache, page_t *page, void *addr) { void *prior; void **object = 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); } } -static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) -{ +static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) { unsigned long flags; irq_save(flags); @@ -220,26 +185,19 @@ 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; - } - else - { + } else { __slub_free(cache, page, addr); } irq_restore(flags); } -void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags) -{ - return slub_alloc(cache, gfpflags); -} +void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags) { return slub_alloc(cache, gfpflags); } -void kmem_cache_free(kmem_cache_t *cache, void *addr) -{ +void kmem_cache_free(kmem_cache_t *cache, void *addr) { page_t *page = 0; page = get_head_page(va2page((unsigned long)addr)); @@ -247,19 +205,16 @@ void kmem_cache_free(kmem_cache_t *cache, void *addr) slub_free(cache, page, addr); } -void *kmalloc(size_t size, gfp_t gfpflags) -{ +void *kmalloc(size_t size, gfp_t gfpflags) { unsigned int i; kmem_cache_t *cache = 0; unsigned long flags; irq_save(flags); - for (i = 0; i < SLUB_INIT_CACHE_SIZE; ++i) - { + for (i = 0; i < SLUB_INIT_CACHE_SIZE; ++i) { kmem_cache_t *p = kmalloc_caches + i; - if (p->objsize >= size) - { + if (p->objsize >= size) { cache = p; break; } @@ -272,8 +227,7 @@ void *kmalloc(size_t size, gfp_t gfpflags) return addr; } -void kfree(void *addr) -{ +void kfree(void *addr) { unsigned long flags; irq_save(flags); @@ -285,12 +239,9 @@ void kfree(void *addr) irq_restore(flags); } -kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) -{ - +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) - return 0; + if (cache == 0) return 0; unsigned long flags; irq_save(flags); @@ -304,18 +255,16 @@ kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) return cache; } -void init_slub_system() -{ +void init_slub_system() { unsigned int i; kmem_cache_t *cache; - for (i = SLUB_MIN_SHIFT; i < SLUB_MAX_SHIFT; ++i) - { + for (i = SLUB_MIN_SHIFT; i < SLUB_MAX_SHIFT; ++i) { cache = kmalloc_caches + i - SLUB_MIN_SHIFT; kmem_cache_init(cache, "kmalloc_old", 1UL << i, KMALLOC_MIN_ALIGN); list_add(&(cache->list), &slub_caches); - //printk("kmem objsize %d\tsize %d \n", cache->objsize, cache->size); + // printk("kmem objsize %d\tsize %d \n", cache->objsize, cache->size); } #if 0