# configuration file generated by Bochs
megs: 128
-plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, iodebug=1
+plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, iodebug=0
config_interface: textconfig
display_library: x
memory: host=128, guest=128
cpuid: vendor_string="GenuineIntel"
cpuid: brand_string=" Intel(R) Pentium(R) 4 CPU "
-usb_uhci: enabled=0
-usb_ohci: enabled=0
+#usb_uhci: enabled=0
+#usb_ohci: enabled=0
print_timestamps: enabled=0
debugger_log: -
magic_break: enabled=1
private_colormap: enabled=0
clock: sync=none, time0=local
# no cmosimage
-ne2k: enabled=0
-pnic: enabled=0
-sb16: enabled=0
+#ne2k: enabled=0
+#pnic: enabled=0
+#sb16: enabled=0
# no loader
log: -
logprefix: %t%e%d
keyboard_paste_delay: 100000
keyboard_mapping: enabled=1, map=/usr/local/share/bochs/keymaps/x11-pc-us.map
user_shortcut: keys=none
-mouse: enabled=0
+#mouse: enabled=0
#mouse: enabled=0, type=ps2, toggle=ctrl+mbutton
TASK_EXITING
};
-typedef union
+typedef union task_union
{
struct
{
};
unsigned char stack[TASK_SIZE];
-} Task, *pTask;
+} task_struct;
-typedef Task task_struct;
+
+typedef task_struct Task;
+typedef task_struct *pTask;
#define ROOT_TSK_PID (1)
extern pTask current;
-extern Task RootTsk;
extern ListHead tsk_list;
#define add_tsk2list(tsk) list_add_tail((&(tsk)->list), &tsk_list)
int sysc_exit(int status)
{
- if(current == &RootTsk)
- panic("Root Task is Exiting...");
+ //if(current == &RootTsk)
+ // panic("Root Task is Exiting...");
/* 先简要实现 */
current->state = TASK_EXITING;
#include <irq.h>
#include <fcntl.h>
#include <stat.h>
+#include <init.h>
#define KRNL_STACK_SIZE 4096
-extern void root_task();
-extern void setup_kernel();
+void root_task_entry();
+void setup_kernel();
TSS tss;
System system;
-static char kernel_stack[KRNL_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
-static char root_task_stack[PAGE_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
+static char __initdata kernel_stack[KRNL_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
int KernelEntry()
{
setup_kernel();
+ char *root_task_user_space_stack = (char *) alloc_pages(0, 0);
+
asm("movl $0x23,%%eax; \
movw %%ax,%%ds; \
movw %%ax,%%es; \
pushl %%ebx; \
pushl $0x282; \
pushl $0x1B; \
- leal root_task,%%eax; \
+ leal root_task_entry,%%eax; \
pushl %%eax; \
- iret;"::"b"(root_task_stack+PAGE_SIZE));
+ iret;"::"b"(root_task_user_space_stack+PAGE_SIZE));
return 0;
}
#if 0
-void root_task()
+void root_task_entry()
{
pid_t pid;
pid = fork();
}
}
#else
-void root_task()
+void root_task_entry()
{
while(1)
{
- //asm("hlt;");
+ syscall0(SYSC_TEST);
}
pid_t pid;
/*
#include "sched.h"
#include "assert.h"
-
-
pTask current;
-Task RootTsk __attribute__((__aligned__(PAGE_SIZE)));
-task_struct* task[TASK_CNT];
-
-#define root_task task[0]
-#define first_task task[0]
+task_struct root_task __attribute__((__aligned__(PAGE_SIZE)));
pid_t get_next_pid()
{
{
int i;
- root_task->pid = get_next_pid();
- root_task->ppid = 0;
+ root_task.pid = get_next_pid();
+ root_task.ppid = 0;
for(i=0; i<NR_OPENS; i++)
- root_task->fps[i] = 0;
+ root_task.fps[i] = 0;
+
+ tss.esp0 = ((unsigned long)&root_task) + sizeof(root_task);
+ root_task.esp0 = tss.esp0;
- /* 这个时候还没有进程开始 */
- root_task->esp0 = tss.esp0;
+ printk("init_root_task tss.esp0 %08x\n", tss.esp0);
- init_tsk_cr3(root_task);
- load_cr3(root_task);
+ //init_tsk_cr3(root_task);
+ //load_cr3(root_task);
- current = root_task;
+ //current = &root_task;
/*
// 栈
void *stack = kmalloc_old(PAGE_SIZE);
inline pTask get_next_tsk()
{
+#if 0
static unsigned int inx = 0;
unsigned int i = 0;
task_struct *tsk = root_task;
}
return tsk;
+#endif
+ return 0;
}
-#if 1
-inline void set_esp0(pTask tsk)
+inline void set_esp0(pTask tsk)
{
tss.esp0 = tsk->esp0;
}
+
inline void switch_to()
{
-
//printk("current:%08x esp0:%08x\n", current, current->esp0);
load_cr3(current);
set_esp0(current);
}
+
inline void context_switch(pTask prev, pTask next)
{
-#if 1
//pTask last;
unsigned long eax, ebx, ecx, edx, esi, edi;
//asm("xchg %bx, %bx");
"1:"
"popl %%ebp;"
"popfl;"
- : [prev_esp] "=m" (prev->esp),
+ : [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_esp] "m" (next->esp),
[next_eip] "m" (next->eip),
[prev] "a" (prev),
[next] "d" (next)
: "memory"
);
-#endif
}
unsigned long schedule()
{
+#if 0
pTask tsk, prev, next;
cli(); // For Safe.
prev = current;
current = next = tsk;
context_switch(prev, next);
-}
#endif
+}
inline void wake_up(pWaitQueue wq)
.extern sysc_handler_table
#endif
.text
-.global syscall_entry
-.global ret_from_fork
+.global syscall_entry
+.global ret_from_fork
+.global sysexit
-/*
- * 0x40 -- %ss
- * 0x3C -- %esp
- * 0x38 -- %eflags
- * 0x34 -- %cs
- * 0x30 -- %eip
- * 0x2C -- sysc_nr
- * 0x28 -- %gs
- * 0x24 -- %fs
- * 0x20 -- %es
- * 0x1C -- %ds
- * 0x18 -- %eax
- * 0x14 -- %ebp
- * 0x10 -- %esi
- * 0x0C -- %edi
- * 0x08 -- %ecx
- * 0x04 -- %edx
- * 0x00 -- %ebx
- */
#if 1
+syscall_entry:
+ movl (%esp),%esp
+
+ SAVE_REGS
+
+ pushfl
+ pushl %ebp
+ pushl %eax
+
+ movw %ss, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %gs
+ movw %ax, %fs
+
+ popl %eax
+
+ cmpl $SYSC_NUM, %eax
+ jae bad_syscnr
+
+ call *sysc_handler_table(,%eax,4)
+
+ret_from_bad_syscnr:
+ popl %ebp
+ popfl
+ RESTORE_REGS
+
+ leal sysexit, %edx
+ movl %ebp, %ecx
+
+ sti /* sysenter会自动清除IF.貌似sysexit不会自动置位 */
+ sysexit
+
+ret_from_fork: /* for compiler now */
+bad_syscnr:
+ call sysc_bad_syscnr
+ jmp ret_from_bad_syscnr
+
+#else
syscall_entry:
/* 此时%esp存的是current的地址(¤t) */
movl (%esp),%esp /* 获得current的值 */
# no need to pay attention on the return address
.section .sysexit
.align 0x1000
-.global sysexit
sysexit:
popl %ebp;
popl %edx;
{
wrmsr(MSR_SYSENTER_CS, SELECTOR_KRNL_CS, 0);
wrmsr(MSR_SYSENTER_EIP, syscall_entry, 0);
- wrmsr(MSR_SYSENTER_ESP, ¤t, 0);
+ wrmsr(MSR_SYSENTER_ESP, &(tss.esp0), 0);
init_sysc_handler_table();
}
int sysc_test()
{
- dump_fd();
+ //dump_fd();
+
+ printk(".");
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)));
-extern void sysexit();
-
void set_page_shared(void *x)
{
unsigned long addr = (unsigned long) x;
pte[get_npt(addr)] |= PAGE_US;
}
+extern void sysexit();
+
void init_paging()
{
unsigned int i;
for(i=delta; i<PDECNT_PER_PAGE; ++i)
{
init_pgd[i] = init_pgd[i-delta];
- init_pgd[i] |= PAGE_US;
}
// paging for user space
phys_addr = . - kernel_virtual_addr_start;
*(.multiboot_header)
*(.text)
- *(.sysexit)
+
+ *(.sysexit) /* last */
}
etext = .;
.data : AT(phys_addr) ALIGN(0x1000)
setup_irqs();
+ setup_tasks();
+
return;
while(1); // TODO MODIFY CODE BELOW
- setup_tasks();
setup_root_dev();
setup_hd();
}
-
void set_tss()
{
pTSS p = &tss;
memset((void *)p, sizeof(TSS), 0);
- p->esp0 = TASK_SIZE + (unsigned long)&RootTsk;
- p->ss0 = SELECTOR_KRNL_DS;
+ 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;