Skip to content

Commit

Permalink
修改了ret_from_fork
Browse files Browse the repository at this point in the history
现在看起来很正常
  • Loading branch information
reticenceji committed Jan 25, 2021
1 parent e0f3a35 commit 3a766a0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
9 changes: 6 additions & 3 deletions arch/riscv/kernel/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ __get_sscratch:
ret

#第一个参数 a0 stack
#第二个参数 a1 kernel_sp
.globl ret_from_fork
.type ret_from_fork,@function
ret_from_fork:
csrr sp,sscratch #继承父进程的用户栈虚拟地址
csrw sscratch,a1 #和父进程有不一样的系统栈地址

ld t0,0x0(a0)
add t0,t0,4
csrw sepc,t0
Expand Down Expand Up @@ -220,10 +224,9 @@ ret_from_fork:
ld x4,0xf8(a0)
ld x3,0x100(a0)
ld ra,0x108(a0)
ld sp,0x110(a0)
ld x10,0xc8(a0) #x10就是a0
#ld sp,0x110(a0)
#ld x10,0xc8(a0) #x10就是a0

csrrw sp,sscratch,sp
mv a0,zero #对于子进程fork返回0
sret

Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/kernel/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void do_page_fault(uintptr_t *regs)
if (current->pid ==1)
{
addr = kmalloc(PAGE_SIZE);
create_mapping(page_base, stval & ~0xFFF, PP(addr), PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
create_mapping(page_base, stval & ~0xFFF, 0x84000000, PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
}
else
create_mapping(page_base, stval & ~0xFFF, PP(current->user_sp), PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
Expand All @@ -40,7 +40,7 @@ void do_page_fault(uintptr_t *regs)
if (current->pid ==1)
{
addr = kmalloc(PAGE_SIZE);
create_mapping(page_base, stval & ~0xFFF, PP(addr), PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
create_mapping(page_base, stval & ~0xFFF, 0x84000000, PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
}
else
create_mapping(page_base, stval & ~0xFFF, PP(current->user_sp), PAGE_SIZE, FLAG_R|FLAG_W|FLAG_V|FLAG_U);
Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
static void copy_mm(struct task_struct *child);
static void copy_mmap(struct task_struct *child);
static struct task_struct* dup_task_struct (struct task_struct *current);
extern void ret_from_fork(uint64 *stack);
extern void ret_from_fork(uint64 *stack,uint64* kernel_sp);

/* TODO 为子进程创建它的task_struct(包括Stack的整个Page,并修改部分内容。如果创建失败(NR_TASK)返回NULL,否则返回它的地址
* stack内容
Expand Down Expand Up @@ -58,6 +58,6 @@ pid_t fork()
/* TODO 主要功能是调用汇编的ret_from_fork */
void fork_ret()
{
ret_from_fork(current->stack);
ret_from_fork(current->stack,current->thread.sp);
return;
}
34 changes: 17 additions & 17 deletions hello.asm
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ Disassembly of section .text.main:
80: fea43023 sd a0,-32(s0)
84: fb5ff0ef jal ra,38 <fork>
88: fea43023 sd a0,-32(s0)
8c: f79ff0ef jal ra,4 <getpid>
90: 00050793 mv a5,a0
94: 00010713 mv a4,sp
98: 00070613 mv a2,a4
9c: 00078593 mv a1,a5
a0: 73000513 li a0,1840
a4: 600000ef jal ra,6a4 <printf>
a8: fe042623 sw zero,-20(s0)
ac: 0100006f j bc <main+0x50>
b0: fec42783 lw a5,-20(s0)
b4: 0017879b addiw a5,a5,1
b8: fef42623 sw a5,-20(s0)
bc: fec42783 lw a5,-20(s0)
c0: 0007871b sext.w a4,a5
c4: fff00793 li a5,-1
c8: fef714e3 bne a4,a5,b0 <main+0x44>
cc: fc1ff06f j 8c <main+0x20>
8c: f79ff0ef jal ra,4 <getpid>
90: 00050793 mv a5,a0
94: 00010713 mv a4,sp
98: 00070613 mv a2,a4
9c: 00078593 mv a1,a5
a0: 73000513 li a0,1840
a4: 600000ef jal ra,6a4 <printf>
a8: fe042623 sw zero,-20(s0)
ac: 0100006f j bc <main+0x50>
b0: fec42783 lw a5,-20(s0)
b4: 0017879b addiw a5,a5,1
b8: fef42623 sw a5,-20(s0)
bc: fec42783 lw a5,-20(s0)
c0: 0007871b sext.w a4,a5
c4: fff00793 li a5,-1
c8: fef714e3 bne a4,a5,b0 <main+0x44>
cc: fc1ff06f j 8c <main+0x20>

Disassembly of section .text.putchar:

Expand Down

0 comments on commit 3a766a0

Please sign in to comment.