Skip to content

Commit 09bb2c4

Browse files
lab4
1 parent 120bf8e commit 09bb2c4

File tree

8 files changed

+81
-15
lines changed

8 files changed

+81
-15
lines changed

include/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define LOG2NENV 10
88
#define NENV (1<<10)
99
#define USTACKTOP (UTOP - 2*BY2PG)
10+
#define UXSTACKTOP (UTOP+2*BY2PG)
1011
#define UTEXT 0x00400000
1112
#define ENVX(envid) ((envid) & (NENV - 1))
1213

include/mmu.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ extern char _start[];
5252
extern char _pg_dir[];
5353
extern void enable_mmu();
5454
extern void boot_mmu_setup();
55-
55+
/*
5656
#define UPAGES 0x80000000
5757
#define UENVS 0x81000000
5858
#define TIMESTACK 0x82000000
59-
#define KERNEL_SP (0x82000000-BY2PG)
59+
#define KERNEL_SP (0x82000000-BY2PG)*/
60+
#define UPAGES 0x50000000
61+
#define UENVS 0x51000000
62+
#define TIMESTACK 0x52000000
63+
#define KERNEL_SP (0x52000000-BY2PG)
6064
//now we need to configure the memory map(kernel)
6165
/*
6266
| -----------------------------------0x82000000

init/main.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern void printel();
1010
extern void irq_vector_init();
1111
extern void enable_interrupt_controller();
1212
extern void user_main_pingpong();
13+
extern void user_main_fktest();
14+
extern void user_main_fktest2();
1315
void main() {
1416
//unsigned long* tmp;
1517
printf(">>>main.c:\tmain is start ...>>>\n");
@@ -24,8 +26,8 @@ void main() {
2426
page_init();
2527
env_init();
2628

27-
env_create_priority((unsigned char *)(&(user_main_pingpong)),0,1);
28-
env_create_priority((unsigned char *)(&(user_main_pingpong)),0,1);
29+
env_create_priority((unsigned char *)(&(user_main_fktest2)),0,1);
30+
//env_create_priority((unsigned char *)(&(user_main_pingpong)),0,1);
2931

3032
irq_vector_init();
3133
//enable_irq();

lib/entry.S

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ error_invalid_el0_32:
153153
el1_irq:
154154
el0_irq:
155155
CLI
156-
ldr x16,=0x82000000//TIMESTACK
156+
ldr x16,=0x52000000//TIMESTACK
157157
mov sp,x16
158158
saveall
159159
@@ -166,7 +166,7 @@ el0_sync:
166166
.extern sys_call_table
167167
.extern invalid_syscall
168168
CLI
169-
ldr x16,=0x81fff000//kernel_sp
169+
ldr x16,=0x51fff000//kernel_sp
170170
mov sp,x16
171171
saveall
172172

lib/syscall_all.c

+23-8
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ int sys_mem_unmap(int sysno, unsigned long envid, unsigned long va)
143143
int sys_env_alloc(void)
144144
//TODO: IMPLEMENT THIS FUNCTION WILL NOT BE USED TEMPORARILY
145145
{
146-
/*
146+
147147
// Your code here.
148148
int r;
149149
struct Env *e;
150+
struct Page* pp;
151+
unsigned long* newpage;
152+
unsigned long currentsp;
150153
// try to alloc a env
151154
r=env_alloc(&e,curenv->env_id);
152155
if(r<0){
@@ -155,14 +158,26 @@ int sys_env_alloc(void)
155158
}
156159
e->env_status=ENV_NOT_RUNNABLE;
157160
bcopy(KERNEL_SP-sizeof(struct Trapframe),(void*)(&(e->env_tf)),sizeof(struct Trapframe));
158-
(e->env_tf).regs[2]=0;
159-
e->env_pri=curenv->env_pri;
160-
//copied from gayhub:is this necessary?
161-
(e->env_tf).pc=e->env_tf.cp0_epc;
161+
(e->env_tf).x[0]=0;
162+
e->env_pri=curenv->env_pri;
163+
//copy a stack?
164+
//I have to use a fake fork
165+
currentsp=ROUNDDOWN(e->env_tf.sp,BY2PG);
166+
for(;currentsp<USTACKTOP;currentsp+=BY2PG){
167+
r=page_alloc(&pp);
168+
if(r<0){
169+
printf("alloc for new stack when fork error\n");
170+
return r;
171+
}
172+
newpage=page2pa(pp);
173+
bcopy(currentsp,newpage,BY2PG);
174+
r=page_insert(e->env_pgdir,pp,currentsp,PTE_V);
175+
}
176+
177+
e->env_status=ENV_RUNNABLE;
162178

163-
return e->env_id;*/
164-
panic("sys_env_alloc not implemented");
165-
return -1;
179+
180+
return e->env_id;
166181
}
167182
int sys_set_env_status(int sysno, unsigned long envid, unsigned long status)
168183
{

user/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ INCLUDES := -I./ -I../ -I../include/
55

66
.PHONY: clean
77

8-
all: syscall_lib.o msyscall.o ipc.o userfunct.o
8+
all: syscall_lib.o msyscall.o ipc.o userfunct.o fork.o
99

1010
clean:
1111
rm -rf *~ *.o

user/fork.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include<helpfunct.h>
2+
#include<mmu.h>
3+
#include<os_printf.h>
4+
#include<env.h>
5+
#include<syscall_lib.h>
6+
int fork(){
7+
return syscall_env_alloc();
8+
}

user/userfunct.c

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
#include<syscall_lib.h>
22
#include<os_printf.h>
33
#include<ipc.h>
4+
void user_main_fktest2(){
5+
int a=0;
6+
int id=0;
7+
8+
if((id=fork())==0)
9+
{
10+
if ((id=fork())==0)
11+
{
12+
a+=3;
13+
for(;;) printf("\t\tthis is child2 :a:%d\n",a);
14+
}
15+
a+=2;
16+
for(;;) printf("\tthis is child :a:%d\n",a);
17+
}
18+
a++;
19+
for(;;) printf("this is father: a:%d\n",a);
20+
}
21+
void user_main_fktest(){
22+
23+
unsigned long r=0;
24+
r=fork();
25+
printf("%x===\n",r);
26+
if(r<0){
27+
panic("failed");
28+
}
29+
if(r==0){
30+
while(1){
31+
printf("i am son\n");
32+
}
33+
}
34+
else if(r>0){
35+
while(1){
36+
printf("i am father\n");
37+
}
38+
}
39+
}
440
void user_main1(){
541
while(1){
642
syscall_putchar('1');

0 commit comments

Comments
 (0)