Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7b96707)
raw | patch | inline | side by side (parent: 7b96707)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Fri, 5 Nov 2021 06:51:10 +0000 (14:51 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Fri, 5 Nov 2021 06:51:10 +0000 (14:51 +0800) |
| .bochsrc | patch | blob | history | |
| include/printk.h | patch | blob | history | |
| kernel/init.c | patch | blob | history | |
| kernel/sched.c | patch | blob | history | |
| kernel/syscall.S | patch | blob | history | |
| mm/bootmem.c | patch | blob | history | |
| mm/mm.c | patch | blob | history | |
| scripts/link.ld | patch | blob | history |
diff --git a/.bochsrc b/.bochsrc
--- a/.bochsrc
+++ b/.bochsrc
#megs: 32
#megs: 16
#megs: 8
-megs: 32
+megs: 8
diff --git a/include/printk.h b/include/printk.h
--- a/include/printk.h
+++ b/include/printk.h
MPL_TEST,
MPL_DEBUG,
MPL_ROOT,
+ MPL_TASK_0,
MPL_TASK_1,
MPL_TASK_2,
MPL_TASK_3,
diff --git a/kernel/init.c b/kernel/init.c
--- a/kernel/init.c
+++ b/kernel/init.c
// int debug_wait_queue_get();
#define __ring3text__ __attribute__((__section__(".ring3.text")))
-#define __ring3data__ __attribute__((__section__(".ring3.data"), __aligned__(PAGE_SIZE)))
-#define __ring3bss__ __attribute__((__section__(".ring3.bss"), __aligned__(PAGE_SIZE)))
-char __ring3data__ ring3_stack[PAGE_SIZE] = {0};
-char __ring3bss__ ring3_bss[PAGE_SIZE + 1234];
int ring3_sysctest();
void __ring3text__ __attribute__((__aligned__(PAGE_SIZE))) ring3_entry() {
while (1) {
- ring3_sysctest();
+ int __sysc_ret__ = 0;
+
+ // 下面这段汇编中的 ring3_entry 和 sysexit_return_address的地址都是内核地址
+ // 然而在用户态用的代码地址是从0x08000000开始的
+ // 因此为了算了systexit返回的正确地址
+ // 需要借助ring3_entry算出sysexit_return_address相对ring3_entry的偏移量
+ // 再把这个偏移量加上0x08000000就是正确的sysexit的地址
+
+ // 必需注意这里的sysexit_return_address并不是sysexit指令返回的地址
+ // sysexit指令返回的是编译在内核中的一段代码地址,这个地址已经被设成内核和用户态共享
+ // 在内核中的那段代码里会利用存在栈上的sysexit_return_address返回到
+ // sysexit_return_address处
+ asm volatile(
+ "leal sysexit_return_address, %%ebx;"
+ "subl %%ecx, %%ebx;"
+ "addl $0x08000000, %%ebx;" // 算出sysexit_return_address在用户态的实际地址
+ "pushl %%ebx;" // 把这个地址保存在栈上,内核sysexit会返回到一段共享代码上
+ // 共享代码会利用保存在栈上的地址返回到sysexit_return_address处
+ "pushl $0;"
+ "pushl $0;"
+ "pushl %%ebp;"
+ "movl %%esp, %%ebp;"
+ "sysenter;"
+ "sysexit_return_address:"
+ : "=a"(__sysc_ret__)
+ : "a"(SYSC_TEST), "c"(ring3_entry));
}
}
-static int __ring3text__ __volatile__ __ring3_syscall0(int nr) {
- int __sysc_ret__ = 0;
- extern void sysenter;
- asm volatile("leal sysenter, %%ebx;call *%%ebx;" : "=a"(__sysc_ret__) : "a"(nr));
- return __sysc_ret__;
-}
-int __ring3text__ _ring3_syscall0(int nr) { return __ring3_syscall0(nr); }
-int __ring3text__ ring3_sysctest() { return _ring3_syscall0(SYSC_TEST); }
-
-void user_task_entry() {
- // printk("user_task_entry: %08x\n", ring3_entry);
-
- // unsigned long ring3_text_page = va2pa(alloc_one_page(0));
- // unsigned long ring3_data_page = va2pa(alloc_one_page(0));
- // unsigned long ring3_bss_page = va2pa(alloc_one_page(0));
+void user_task_entry(char *name) {
+ strcpy(current->name, name);
unsigned long ring3_text_page = va2pa(ring3_entry);
- unsigned long ring3_data_page = va2pa(ring3_stack);
unsigned long ring3_bss_page = va2pa(alloc_one_page(0));
unsigned long *pt_text_page = (unsigned long *)(alloc_one_page(0));
- unsigned long *pt_data_page = (unsigned long *)(alloc_one_page(0));
unsigned long *pt_bss_page = (unsigned long *)(alloc_one_page(0));
+
unsigned long *p = (unsigned long *)(pa2va(current->cr3));
printk("page dir : %x %x %x %x\n", p, pt_text_page, ring3_text_page);
printk("pt bss page %x %x", pt_bss_page, ring3_bss_page);
// text: 0x0800_0000
- // data: 0x2000_0000
// bss: 0x3000_0000
unsigned long text_at = 0x08000000;
- unsigned long data_at = 0x20000000;
unsigned long bbs_at = 0x30000000;
- p[text_at >> 22] = (unsigned long)va2pa(pt_text_page) | PAGE_P | PAGE_WR | PAGE_US;
- pt_text_page[0] = ring3_text_page | 7;
- p[data_at >> 22] = (unsigned long)va2pa(pt_data_page) | PAGE_P | PAGE_WR | PAGE_US;
- pt_data_page[0] = ring3_data_page | 7;
+ p[text_at >> 22] = (unsigned long)va2pa(pt_text_page) | PAGE_P | PAGE_US;
+ pt_text_page[0] = ring3_text_page | PAGE_P | PAGE_US;
p[bbs_at >> 22] = (unsigned long)va2pa(pt_bss_page) | PAGE_P | PAGE_WR | PAGE_US;
- pt_bss_page[0] = ring3_bss_page | 7;
+ pt_bss_page[0] = ring3_bss_page | PAGE_P | PAGE_WR | PAGE_US;
//
LoadCR3(current->cr3);
asm volatile("sysexit;" ::"d"(0x08000000), "c"(0x30000000 + PAGE_SIZE - 100));
}
-void init_task_entry() {
+void init_task_entry(char *name) {
int cnt = 0;
pid_t id = sysc_getpid();
+ strcpy(current->name, name);
+
// 赋予不同的优先级
current->priority = id * 30;
if (current->priority <= 0) {
while (1) {
// sysc_test();
- printl(MPL_TASK_1 + id - 1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
+ // printl(MPL_TASK_0 + id, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
// printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
int v = 0; // debug_wait_queue_get();
// printk("task:%d wait queue get %d\n", id, v);
regs.fs = SELECTOR_KRNL_DS;
regs.gs = SELECTOR_KRNL_DS;
+ regs.ebx = (unsigned long)name; // 用ebx传递参数
+
int pid = do_fork(®s, FORK_KRNL);
printk("kernel[%s] task pid is %d\n", name, pid);
int cnt = 0;
while (1) {
// sysc_test();
- printl(MPL_ROOT, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++);
+ // printl(MPL_TASK_0, "root:0 [%08x] weight %d cnt %d", current, root_task.weight, cnt++);
// printk("root:0 [%08x] weight %d cnt %d", current, current->weight, cnt++);
asm("sti;hlt;");
// asm("nop;nop;nop;");
diff --git a/kernel/sched.c b/kernel/sched.c
--- a/kernel/sched.c
+++ b/kernel/sched.c
unsigned long iflags;
irq_save(iflags);
- printl(MPL_ROOT, "root:%d [%08x] cnt %u", root_task.pid, &root_task, root_task.cnt);
+ // printl(MPL_ROOT, "root:%d [%08x] cnt %u", root_task.pid, &root_task, root_task.cnt);
float min_ratio = 1.0;
list_for_each_safe(pos, t, &all_tasks) {
p = list_entry(pos, task_union, list);
- printl(MPL_ROOT + p->pid, "task:%d [%08x] state %s cnt %u", p->pid, p, task_state(p->state), p->cnt);
-
if (p->state != TASK_RUNNING) {
continue;
}
if (prev != next) {
// printk("switch to: %s:%d\n", next->name, next->pid);
+
+ list_for_each_safe(pos, t, &all_tasks) {
+ p = list_entry(pos, task_union, list);
+ printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d cnt %u", sel == p ? ">" : " ", p->name,
+ p->pid, p, task_state(p->state), p->weight, p->cnt);
+ }
context_switch(prev, next);
}
}
diff --git a/kernel/syscall.S b/kernel/syscall.S
--- a/kernel/syscall.S
+++ b/kernel/syscall.S
addl $12, %esp
popfl
addl $8, %esp
+
+ # 用ebx传递参数
+ pushl %ebx
call *%edx
+ addl $4, %esp
# 还没有实现do_exit
#call do_exit
# this page is shared by the kernel and userspace
# the reason why i use this method is because of that the user program
# no need to pay attention on the return address
-.global sysenter
-.section .sysenter
-.align 0x1000
-sysenter:
- pushl %ecx;
- pushl %edx;
- pushl %ebp;
- movl %esp, %ebp;
- sysenter;
-
-
-
.global sysexit
.section .sysexit
.align 0x1000
diff --git a/mm/bootmem.c b/mm/bootmem.c
--- a/mm/bootmem.c
+++ b/mm/bootmem.c
u8 data = op == 0 ? 0x00 : 0xFF;
// 先设置头部不是从单个字节开始的比特
- unsigned int i = 0; // 这个变更不能放到for循环里定义
+ unsigned int i = 0; // 这个变量不能放到for循环里定义
for (i = bgn_pfn; i < end_pfn && (i % 8 != 0); i++) {
bit_func(i, bootmem_data.bitmap);
}
// 算出中间的整字节数
unsigned int bytes = (end_pfn - i) / 8;
- // 直接清零
+ // 直接清零或设置
memset((char *)(bootmem_data.bitmap) + (i / 8), data, bytes);
// 最后设置尾部不是整字节的比特
--- a/mm/mm.c
+++ b/mm/mm.c
}
// paging for user space
- extern void sysenter();
extern void sysexit();
- set_page_shared(sysenter);
set_page_shared(sysexit);
LoadCR3(va2pa(init_pgd));
diff --git a/scripts/link.ld b/scripts/link.ld
--- a/scripts/link.ld
+++ b/scripts/link.ld
boot/multiboot.S.o(.text)
*(.multiboot_header)
*(.text)
-
- *(.sysenter) /* last */
+ *(.ring3.text);
*(.sysexit) /* last */
}
etext = .;
ebss = .;
- .ring3.text : AT(phys_addr) ALIGN(0x1000)
- {
- phys_addr = . - kernel_virtual_addr_start;
- *(.ring3.text);
- }
-
- .ring3.data : AT(phys_addr) ALIGN(0x1000)
- {
- phys_addr = . - kernel_virtual_addr_start;
- *(.ring3.data);
- }
-
- .ring3.bss : AT(phys_addr) ALIGN(0x1000)
- {
- phys_addr = . - kernel_virtual_addr_start;
- *(.ring3.bss);
- }
-
end = .;
_end = .;
kernel_end = .;