From 5a967402a2c4f68846fdaff8c8d6b4f9b5e44650 Mon Sep 17 00:00:00 2001 From: hinus Date: Wed, 9 Jun 2021 23:37:03 +0800 Subject: [PATCH] Title: Set 'esp' register Issue: https://gitee.com/hinus/linux_kernel_011/issues/I3V35G Description: Set 'esp' register, make it point to user_stack. --- Makefile | 70 ++++++++++++++++++++++++------------------------- kernel/Makefile | 11 +++++--- kernel/head.S | 1 + kernel/main.c | 9 ++++++- kernel/sched.c | 10 +++++++ 5 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 kernel/sched.c diff --git a/Makefile b/Makefile index c8f6439..99cfe01 100644 --- a/Makefile +++ b/Makefile @@ -1,35 +1,35 @@ -AS := as -LD := ld -m elf_x86_64 - -LDFLAG := -Ttext 0x0 -s --oformat binary - -image : linux.img - -linux.img : tools/build bootsect setup kernel/system - ./tools/build bootsect setup kernel/system > $@ - -tools/build : tools/build.c - gcc -o $@ $< - -kernel/system : - cd kernel; make system; cd .. - -bootsect : bootsect.o - $(LD) $(LDFLAG) -o $@ $< - -bootsect.o : bootsect.S - $(AS) -o $@ $< - -setup : setup.o - $(LD) $(LDFLAG) -e _start_setup -o $@ $< - -setup.o : setup.S - $(AS) -o $@ $< -clean: - rm *.o - rm bootsect - rm setup - rm tools/build - rm linux.img - cd kernel; make clean; cd .. - +AS := as +LD := ld -m elf_x86_64 + +LDFLAG := -Ttext 0x0 -s --oformat binary + +image : linux.img + +linux.img : tools/build bootsect setup kernel/system + ./tools/build bootsect setup kernel/system > $@ + +tools/build : tools/build.c + gcc -o $@ $< + +kernel/system : kernel/head.S kernel/*.c + cd kernel; make system; cd .. + +bootsect : bootsect.o + $(LD) $(LDFLAG) -o $@ $< + +bootsect.o : bootsect.S + $(AS) -o $@ $< + +setup : setup.o + $(LD) $(LDFLAG) -e _start_setup -o $@ $< + +setup.o : setup.S + $(AS) -o $@ $< +clean: + rm *.o + rm bootsect + rm setup + rm tools/build + rm linux.img + cd kernel; make clean; cd .. + diff --git a/kernel/Makefile b/kernel/Makefile index 59e0061..2f1973f 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,18 +1,21 @@ GCC := gcc -CCFLAG := -I../include -nostdinc -Wall -fomit-frame-pointer -c -LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_x86_64 +CCFLAG := -I../include -nostdinc -Wall -fomit-frame-pointer -c -m32 +LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include -OBJS := head.o main.o +OBJS := head.o main.o sched.o system: $(OBJS) $(LD) $(LDFLAG) -e startup_32 -o $@ $^ head.o : head.S - $(GCC) -traditional -c -o $@ $< + $(GCC) -m32 -traditional -c -o $@ $< main.o : main.c $(GCC) $(CCFLAG) -o $@ $< +sched.o : sched.c + $(GCC) $(CCFLAG) -o $@ $< + clean : rm *.o rm system diff --git a/kernel/head.S b/kernel/head.S index 077f7c5..4f74b18 100644 --- a/kernel/head.S +++ b/kernel/head.S @@ -9,6 +9,7 @@ startup_32: movw %ax, %fs movw %ax, %gs + lss stack_start, %esp call setup_idt call setup_gdt diff --git a/kernel/main.c b/kernel/main.c index 2e4356f..014708a 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -2,7 +2,14 @@ void main(void) { - __asm__("int $0x80 \n\r"::); + char* p1 = "hello world!\0"; + short* p = (short*)0xb8000; + while(*p1) { + *p++ = 0xf00 | (*p1++); + } + + __asm__("int $0x80\n\r"::); + __asm__ __volatile__( "loop:\n\r" "jmp loop" diff --git a/kernel/sched.c b/kernel/sched.c new file mode 100644 index 0000000..97a9e99 --- /dev/null +++ b/kernel/sched.c @@ -0,0 +1,10 @@ +#define PAGE_SIZE 4096 + +long user_stack[PAGE_SIZE >> 2]; + +struct +{ + long *a; + short b; +} stack_start = {&user_stack[PAGE_SIZE >> 2], 0x10}; + -- Gitee