Skip to content

Commit e0c7abe

Browse files
committed
Chapter 5
1 parent 50637e7 commit e0c7abe

24 files changed

+475
-150
lines changed

.github/workflows/github-autotest.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: auto-test
22

3-
on:
3+
on:
44
push:
55

66
jobs:
@@ -12,11 +12,11 @@ jobs:
1212
image: duskmoon/dev-env:ucore-ci
1313
steps:
1414
- uses: actions/checkout@v3
15-
- run: git clone https://github.com/LearningOS/uCore-Tutorial-Checker-2023S.git ucore-tutorial-ci
15+
- run: git clone https://github.com/LearningOS/uCore-Tutorial-Checker-2023S.git ucore-tutorial-ci
1616
- run: git clone https://github.com/LearningOS/uCore-Tutorial-Test-2023S.git ucore-tutorial-ci/workplace/user
1717
- name: run test
1818
id: tester
19-
run: cd ucore-tutorial-ci && make test passwd=${{ secrets.BASE_TEST_TOKEN }} CHAPTER=`echo ${GITHUB_REF##*/} | grep -oP 'ch\K[0-9]'` | tee ../output.txt
19+
run: cd ucore-tutorial-ci && make test passwd=${{ secrets.BASE_TEST_TOKEN }} CHAPTER=`echo ${GITHUB_REF##*/} | grep -oP 'ch\K[0-9]'` > ../output.txt && cat ../output.txt
2020
- name: end
2121
id: end
2222
run: cat output.txt | grep "Test passed" | grep -oP "\d{1,}/\d{1,}" | xargs -i echo "points={}" >> $GITHUB_OUTPUT

Makefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.PHONY: clean build user
2-
all: build_kernel
1+
.PHONY: clean build user run debug test .FORCE
2+
all: build
33

44
K = os
55

@@ -57,6 +57,9 @@ ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
5757
CFLAGS += -fno-pie -nopie
5858
endif
5959

60+
# empty target
61+
.FORCE:
62+
6063
LDFLAGS = -z max-page-size=4096
6164

6265
$(AS_OBJS): $(BUILDDIR)/$K/%.o : $K/%.S
@@ -73,10 +76,12 @@ $(HEADER_DEP): $(BUILDDIR)/$K/%.d : $K/%.c
7376
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
7477
rm -f $@.$$$$
7578

79+
INIT_PROC ?= usershell
80+
7681
os/link_app.o: $K/link_app.S
77-
os/link_app.S: scripts/pack.py
78-
@$(PY) scripts/pack.py
79-
os/kernel_app.ld: scripts/kernelld.py
82+
os/link_app.S: scripts/pack.py .FORCE
83+
@$(PY) scripts/pack.py $(INIT_PROC)
84+
os/kernel_app.ld: scripts/kernelld.py .FORCE
8085
@$(PY) scripts/kernelld.py
8186

8287
build: build/kernel
@@ -89,7 +94,6 @@ build/kernel: $(OBJS) os/kernel_app.ld
8994

9095
clean:
9196
rm -rf $(BUILDDIR) os/kernel_app.ld os/link_app.S
92-
make -C user clean
9397

9498
# BOARD
9599
BOARD ?= qemu
@@ -118,8 +122,6 @@ debug: build/kernel .gdbinit
118122

119123
CHAPTER ?= $(shell git rev-parse --abbrev-ref HEAD | grep -oP 'ch\K[0-9]')
120124

121-
BASE ?= 0
122-
123125
user:
124126
make -C user CHAPTER=$(CHAPTER) BASE=$(BASE)
125127

os/console.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ void consputc(int c)
99
void console_init()
1010
{
1111
// DO NOTHING
12+
}
13+
14+
int consgetc()
15+
{
16+
return console_getchar();
1217
}

os/console.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CONSOLE_H
33

44
void consputc(int);
5+
int consgetc();
56
void console_init();
67

78
#endif // CONSOLE_H

os/const.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ enum {
3131

3232
// memory layout end
3333

34+
#define MAX_APP_NUM (32)
3435
#define MAX_STR_LEN (200)
36+
#define IDLE_PID (0)
3537

3638
#endif // CONST_H

os/defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
#define MIN(a, b) (a < b ? a : b)
1818
#define MAX(a, b) (a > b ? a : b)
1919

20+
#define NULL ((void *)0)
21+
2022
#endif // DEF_H

os/kalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void kfree(void *pa)
4545
// Allocate one 4096-byte page of physical memory.
4646
// Returns a pointer that the kernel can use.
4747
// Returns 0 if the memory cannot be allocated.
48-
void *kalloc(void)
48+
void *kalloc()
4949
{
5050
struct linklist *l;
5151
l = kmem.freelist;

os/kalloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef KALLOC_H
22
#define KALLOC_H
33

4-
void *kalloc(void);
4+
void *kalloc();
55
void kfree(void *);
6-
void kinit(void);
6+
void kinit();
77

88
#endif // KALLOC_H

os/kernelld.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*(.data)
3939
''')
4040
for (idx, _) in enumerate(apps):
41-
f.write(' . = ALIGN(0x1000);\n')
41+
f.write(' . = ALIGN(0x8);\n')
4242
f.write(' *(.data.app{})\n'.format(idx))
4343
f.write(
4444
'''

os/loader.c

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,101 @@
44

55
static int app_num;
66
static uint64 *app_info_ptr;
7-
extern char _app_num[];
8-
9-
// Count finished programs. If all apps exited, shutdown.
10-
int finished()
11-
{
12-
static int fin = 0;
13-
if (++fin >= app_num)
14-
panic("all apps over");
15-
return 0;
16-
}
7+
extern char _app_num[], _app_names[], INIT_PROC[];
8+
char names[MAX_APP_NUM][MAX_STR_LEN];
179

1810
// Get user progs' infomation through pre-defined symbol in `link_app.S`
1911
void loader_init()
2012
{
13+
char *s;
2114
app_info_ptr = (uint64 *)_app_num;
2215
app_num = *app_info_ptr;
2316
app_info_ptr++;
17+
s = _app_names;
18+
printf("app list:\n");
19+
for (int i = 0; i < app_num; ++i) {
20+
int len = strlen(s);
21+
strncpy(names[i], (const char *)s, len);
22+
s += len + 1;
23+
printf("%s\n", names[i]);
24+
}
2425
}
2526

26-
pagetable_t bin_loader(uint64 start, uint64 end, struct proc *p)
27+
int get_id_by_name(char *name)
2728
{
28-
pagetable_t pg = uvmcreate();
29-
if (mappages(pg, TRAPFRAME, PGSIZE, (uint64)p->trapframe,
30-
PTE_R | PTE_W) < 0) {
31-
panic("mappages fail");
32-
}
33-
if (!PGALIGNED(start)) {
34-
panic("user program not aligned, start = %p", start);
35-
}
36-
if (!PGALIGNED(end)) {
37-
// Fix in ch5
38-
warnf("Some kernel data maybe mapped to user, start = %p, end = %p",
39-
start, end);
29+
for (int i = 0; i < app_num; ++i) {
30+
if (strncmp(name, names[i], 100) == 0)
31+
return i;
4032
}
41-
end = PGROUNDUP(end);
42-
uint64 length = end - start;
43-
if (mappages(pg, BASE_ADDRESS, length, start,
44-
PTE_U | PTE_R | PTE_W | PTE_X) != 0) {
45-
panic("mappages fail");
33+
warnf("Cannot find such app %s", name);
34+
return -1;
35+
}
36+
37+
int bin_loader(uint64 start, uint64 end, struct proc *p)
38+
{
39+
if (p == NULL || p->state == UNUSED)
40+
panic("...");
41+
void *page;
42+
uint64 pa_start = PGROUNDDOWN(start);
43+
uint64 pa_end = PGROUNDUP(end);
44+
uint64 length = pa_end - pa_start;
45+
uint64 va_start = BASE_ADDRESS;
46+
uint64 va_end = BASE_ADDRESS + length;
47+
for (uint64 va = va_start, pa = pa_start; pa < pa_end;
48+
va += PGSIZE, pa += PGSIZE) {
49+
page = kalloc();
50+
if (page == 0) {
51+
panic("...");
52+
}
53+
memmove(page, (const void *)pa, PGSIZE);
54+
if (pa < start) {
55+
memset(page, 0, start - va);
56+
} else if (pa + PAGE_SIZE > end) {
57+
memset(page + (end - pa), 0, PAGE_SIZE - (end - pa));
58+
}
59+
if (mappages(p->pagetable, va, PGSIZE, (uint64)page,
60+
PTE_U | PTE_R | PTE_W | PTE_X) != 0)
61+
panic("...");
4662
}
47-
p->pagetable = pg;
48-
uint64 ustack_bottom_vaddr = BASE_ADDRESS + length + PAGE_SIZE;
49-
if (USTACK_SIZE != PAGE_SIZE) {
50-
// Fix in ch5
51-
panic("Unsupported");
63+
// map ustack
64+
p->ustack = va_end + PAGE_SIZE;
65+
for (uint64 va = p->ustack; va < p->ustack + USTACK_SIZE;
66+
va += PGSIZE) {
67+
page = kalloc();
68+
if (page == 0) {
69+
panic("...");
70+
}
71+
memset(page, 0, PGSIZE);
72+
if (mappages(p->pagetable, va, PGSIZE, (uint64)page,
73+
PTE_U | PTE_R | PTE_W) != 0)
74+
panic("...");
5275
}
53-
mappages(pg, ustack_bottom_vaddr, USTACK_SIZE, (uint64)kalloc(),
54-
PTE_U | PTE_R | PTE_W | PTE_X);
55-
p->ustack = ustack_bottom_vaddr;
56-
p->trapframe->epc = BASE_ADDRESS;
5776
p->trapframe->sp = p->ustack + USTACK_SIZE;
77+
p->trapframe->epc = va_start;
5878
p->max_page = PGROUNDUP(p->ustack + USTACK_SIZE - 1) / PAGE_SIZE;
5979
p->program_brk = p->ustack + USTACK_SIZE;
6080
p->heap_bottom = p->ustack + USTACK_SIZE;
61-
return pg;
81+
p->state = RUNNABLE;
82+
return 0;
83+
}
84+
85+
int loader(int app_id, struct proc *p)
86+
{
87+
return bin_loader(app_info_ptr[app_id], app_info_ptr[app_id + 1], p);
6288
}
6389

6490
// load all apps and init the corresponding `proc` structure.
65-
int run_all_app()
91+
int load_init_app()
6692
{
67-
for (int i = 0; i < app_num; ++i) {
68-
struct proc *p = allocproc();
69-
tracef("load app %d", i);
70-
bin_loader(app_info_ptr[i], app_info_ptr[i + 1], p);
71-
p->state = RUNNABLE;
72-
/*
73-
* LAB1: you may need to initialize your new fields of proc here
74-
*/
93+
int id = get_id_by_name(INIT_PROC);
94+
if (id < 0)
95+
panic("Cannpt find INIT_PROC %s", INIT_PROC);
96+
struct proc *p = allocproc();
97+
if (p == NULL) {
98+
panic("allocproc\n");
7599
}
100+
debugf("load init proc %s", INIT_PROC);
101+
loader(id, p);
102+
add_task(p);
76103
return 0;
77104
}

os/loader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
#define LOADER_H
33

44
#include "const.h"
5+
#include "proc.h"
56
#include "types.h"
67

78
int finished();
89
void loader_init();
9-
int run_all_app();
10+
int load_init_app();
11+
int loader(int, struct proc *);
12+
int get_id_by_name(char *);
1013

1114
#define BASE_ADDRESS (0x1000)
1215
#define USTACK_SIZE (PAGE_SIZE)

os/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main()
2121
loader_init();
2222
trap_init();
2323
timer_init();
24-
run_all_app();
24+
load_init_app();
2525
infof("start scheduler!");
2626
scheduler();
2727
}

0 commit comments

Comments
 (0)