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