initrd
initfs
rootfs
+**/init
$(CC) $(CFLAGS) hello.c -o hello.o
$(LD) -m elf_i386 $(LIBC_OBJS) hello.o -o hello
+init: init.o
+ $(CC) $(CFLAGS) init.S -o init.o
+ #$(LD) -m elf_i386 $(LIBC_OBJS) hello.o -o hello
+ #$(LD) -m elf_i386 init.o -o init
+ #$(LD) -m elf_i386 -Ttext 0xc017f000 --oformat binary init.o -o init
+ $(LD) -m elf_i386 -Ttext 0x0017f000 --oformat binary init.o -o init
.PHONY:clean
clean:
--- /dev/null
+# 仅用来调试
+
+.global _start
+.section .text
+_start:
+ nop;
+ nop;
+ nop;
+ #leal 1f,%eax;
+ #pushl %eax
+ pushl $1f; # 这里push 1f和$1f意义不一样
+ pushl %ecx;
+ pushl %edx;
+ pushl %ebp;
+ nop;
+ nop;
+ movl %esp,%ebp;
+ nop;
+ movl $123,%ebx
+ movl $5,%eax # SYSC_WAIT
+ sysenter;
+1:
+ nop;
+ nop;
+ nop;
+ movl $300000000, %ecx
+2:
+ nop
+ nop
+ nop
+ nop
+ nop
+ loop 2b
+ jmp _start
printk("module 0x%08x - 0x%08x size %u cmdline %s\n", m->mod_start, m->mod_end, m->size, m->cmdline);
boot_params.boot_module_begin = (void *)m->mod_start;
boot_params.boot_module_end = (void *)m->mod_end;
-
+#if 0
const uint32_t mod_magic = *(uint32_t *)(mod_start + 0);
const uint32_t mod_head_size = *(uint32_t *)(mod_start + 4);
const uint32_t mod_timestamp = *(uint32_t *)(mod_start + 8);
printk("%02X ", c);
}
printk("\n");
+
}
+#endif
break;
case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
mminfo = (struct multiboot_tag_basic_meminfo *)tag;
///
#define MAX_FILES 1024
-static file_t g_files[MAX_FILES] = {
- 0,
-};
+file_t g_files[MAX_FILES];
void init_file(file_t *fp) {
fp->f_dentry = NULL;
#b init_task_entry
+#b task_init.c:216
+#b *0xC017F000
+#b *0x0017F000
c
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);
void *kmem_cache_zalloc(kmem_cache_t *cache, gfp_t gfpflags);
+
+#define VM_READ 0x00000001
+#define VM_WRITE 0x00000002
+#define VM_EXEC 0x00000004
+#define VM_GROW_UP 0x10000000
+#define VM_GROW_DOWN 0x20000000
+
+typedef struct vm_area {
+ uint32_t vm_bgn;
+ uint32_t vm_end;
+
+ uint32_t vm_flags;
+
+ struct vma *vm_next;
+} vm_area_t;
ERRORCODE (SegNotPresent)
ERRORCODE (StackFault)
ERRORCODE (GeneralProtection)
-ERRORCODE (PageFault)
+ERRORCODE (_page_fault)
NOERRCODE (CoprocError)
pde_dst[i] = pde_src[i] & (~PDE_RW);
#else
// 这里不用再为每个PDE拷贝一次PageTable,只需要拷贝PageDirectory并将其低于768的写权限去掉
- // 同时需要修改缺页异常doPageFault的逻辑
+ // 同时需要修改缺页异常do_page_fault的逻辑
if (PAGE_ALIGN(spde) != 0) {
dpde = page2va(alloc_one_page(0));
assert(dpde != 0);
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 (1); \
} while (0);
void doDivideError(pt_regs_t regs) { DIE_MSG(); }
void do_no_page(void *);
void do_wp_page(void *);
-void doPageFault(pt_regs_t regs) {
+void do_page_fault(pt_regs_t regs) {
#if 0
US RW P - Description
0 0 0 - Supervisory process tried to read a non-present page entry
1 0 1 - User process tried to read a page and caused a protection fault
1 1 0 - User process tried to write to a non-present page entry
1 1 1 - User process tried to write a page and caused a protection fault
+#endif
+#if 0
+ bit 0: 0 non-present page entry; 1 protection fault
+ bit 1: 0 read; 1 write
+ bit 2: 0 supervisor mode; 1 user mode
#endif
// DIE_MSG();
void *addr;
// assert(errcode != 2 && errcode != 6);
+ printk("errcode %x addr %x\n", errcode, addr);
if ((errcode & PAGE_P) == 0) {
do_no_page(addr);
} else {
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(0x0E, TRAP_GATE, PRIVILEGE_KRNL, _page_fault);
set_sys_int(0x10, TRAP_GATE, PRIVILEGE_KRNL, CoprocError);
for (int i = 0x11; i < 0x20; i++) {
+#include <boot.h>
#include <disk.h>
#include <fcntl.h>
#include <io.h>
kernel_task("tskC", taskC_entry, NULL);
#endif
+#if 1
+ void *mod_start = pa2va(boot_params.boot_module_begin);
+
+ mod_start = (void *)va2pa(mod_start);
+
+ unsigned long text_at = (unsigned long)mod_start;
+ text_at &= 0xFFFFF000;
+
+ int pgd_index = (text_at >> 22) & 0x3FF;
+ int pt_index = (text_at >> 12) & 0x3FF;
+
+ unsigned long *pgd = (unsigned long *)(pa2va(current->cr3));
+
+ unsigned long *pt_page = (unsigned long *)page2va(alloc_one_page(0));
+ memset(pt_page, 0, PAGE_SIZE);
+
+ pgd[pgd_index] = va2pa(pt_page) | PAGE_P | PAGE_WR | PAGE_US;
+
+ pt_page[pt_index] = text_at | PAGE_P | PAGE_WR | PAGE_US;
+
+ printk("RING3 ENTRY %x page %x pgd inx %u pt inx %u\n", mod_start, text_at, pgd_index, pt_index);
+
+ LoadCR3(current->cr3);
+
+ asm("sysexit;" ::"d"(mod_start), "c"(mod_start + PAGE_SIZE - 4));
+#else
+ void *mod_start = pa2va(boot_params.boot_module_begin);
+ printk("RING3 ENTRY %x\n", mod_start);
+
+ unsigned long text_at = mod_start;
+ int pgd_index = (text_at >> 22) & 0x3FF;
+ int pt_index = (text_at >> 12) & 0x3FF;
+ unsigned long *pgd = (unsigned long *)(pa2va(current->cr3));
+ pgd[pgd_index] = pgd[pgd_index] | PAGE_WR | PAGE_US;
+
+ unsigned long pgde = pgd[pgd_index];
+ pgde &= 0xFFFFF000;
+
+ unsigned long *pt = (unsigned long *)pa2va(pgde);
+ pt[pt_index] = pt[pt_index] | PAGE_WR | PAGE_US;
+
+ asm("sysexit;" ::"d"(mod_start), "c"(mod_start + PAGE_SIZE - 4));
+#endif
+
while (1) {
+ asm("nop;");
+ asm("nop;");
+ asm("nop;");
+ asm("nop;");
sysc_wait(1);
}
}
-#include <boot.h>
void init_rootfs() {
+#if 0
void *mod_start = pa2va(boot_params.boot_module_begin);
const uint32_t mod_magic = *(uint32_t *)(mod_start + 0);
printk("\n");
}
}
+#endif
}
kernel_task("init", init_task_entry, NULL);
+ strcpy(current->name, "idle");
+
current->priority = 1;
while (1) {
asm("hlt;");
int _syscall3(int nr, unsigned long a, unsigned long b, unsigned long c) { return __syscall3(nr, a, b, c); }
-int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d) { return __syscall4(nr, a, b, c, d); }
+int _syscall4(int nr, unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
+ return __syscall4(nr, a, b, c, d);
+}
files[0]="KERNEL.ELF:$grub2_boot_dir/Kernel"
files[1]="scripts/iso.grub.cfg:$grub2_boot_dir/grub/grub.cfg"
files[2]="rootfs:$grub2_boot_dir/rootfs"
-
+files[3]="bin/init:$grub2_boot_dir/init"
for i in "${!files[@]}"; do
file_line="${files[$i]}"
#set gfxpayload=1024x768x32
#insmod all_video
multiboot2 /boot/Kernel root=hda7 delay=2
- module2 /boot/rootfs rootfs
+ #module2 /boot/rootfs rootfs
+ module2 /boot/init init
boot
}