Skip to content

Commit ed4e9f6

Browse files
committed
Add two user program for testing fork and mailbox call, all success, need fix sync_handler_exception issue
1 parent 26aee7e commit ed4e9f6

File tree

13 files changed

+202
-39
lines changed

13 files changed

+202
-39
lines changed

lab6/kernel/include/mbox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
/* a properly aligned buffer */
27-
extern unsigned int mbox[36];
27+
extern unsigned int mbox[8];
2828

2929
#define MBOX_REQUEST 0
3030

lab6/kernel/include/mmu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#define VA2PA(addr) ((uint64_t)(addr) & (uint64_t)0x0000ffffffffffff)
3737
#define PA2VA(addr) ((uint64_t)(addr) | (uint64_t)0xffff000000000000)
3838

39-
#define PERIPHERAL_START 0x3c100000
40-
#define PERIPHERAL_END 0x3c300000
39+
#define PERIPHERAL_START 0x3c000000
40+
#define PERIPHERAL_END 0x3f000000
4141

4242
void init_page_table(thread_info *thread, uint64_t **table);
4343
void update_page_table(thread_info *thread, uint64_t virtual_addr,

lab6/kernel/include/thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define STACK_SIZE 4096
66
#define USER_STACK_BASE ((uint64_t)0xffffffffb000)
77
#define USER_PROGRAM_BASE 0x80000
8-
#define USER_PROGRAM_SIZE (256 * kb)
8+
#define USER_PROGRAM_SIZE (512 * kb)
99

1010
#define MAX_PAGE_FRAME_PER_THREAD 1000
1111

lab6/kernel/src/exception.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ void sync_handler_lowerEL_64(uint64_t sp) {
6161
const char **argv = (const char **)trap_frame->x[1];
6262
exec(program_name, argv);
6363
} else if (iss == 4) { // fork
64-
printf("[fork]\n");
64+
// printf("[fork]\n");
6565
fork(sp);
6666
} else if (iss == 5) { // exit
6767
exit();
6868
} else if (iss == 6) { // mbox_call
69-
printf("[mbox_call]\n");
69+
// printf("[mbox_call]\n");
7070
thread_info *cur = get_current();
7171
unsigned int * mbox_user_va = (unsigned int *)trap_frame->x[1];
7272
unsigned int * mbox_user_pa = (unsigned int *)el0_VA2PA(cur,(uint64_t)mbox_user_va);
73-
printf("mbox_user_va:%p\n",mbox_user_va);
74-
printf("mbox_user_pa :%p\n",mbox_user_pa);
73+
// printf("mbox_user_va:%p\n",mbox_user_va);
74+
// printf("mbox_user_pa :%p\n",mbox_user_pa);
7575
unsigned int * mbox_kernel_va = (unsigned int *) PA2VA(mbox_user_pa);
76-
printf("mbox_kernel_va :%p\n",mbox_kernel_va); // trap_frame->x[0] = mbox_call(trap_frame->x[0],(unsigned int *)trap_frame->x[1]);
76+
// printf("mbox_kernel_va :%p\n",mbox_kernel_va); // trap_frame->x[0] = mbox_call(trap_frame->x[0],(unsigned int *)trap_frame->x[1]);
7777
trap_frame->x[0] = mbox_call(trap_frame->x[0],mbox_kernel_va);
7878
} else if (iss == 7) { // kill
7979
kill((int)trap_frame->x[0]);

lab6/kernel/src/mbox.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#include "gpio.h"
2727
#include "mmu.h"
2828
#include "printf.h"
29+
#include "mbox.h"
2930
/* mailbox message buffer */
30-
volatile unsigned int __attribute__((aligned(16))) mbox[36];
31+
unsigned int __attribute__((aligned(16))) mbox[8];
3132

3233
#define VIDEOCORE_MBOX (MMIO_BASE+0x0000B880)
3334
#define MBOX_READ ((volatile unsigned int*)(VIDEOCORE_MBOX+0x0))
@@ -45,7 +46,8 @@ volatile unsigned int __attribute__((aligned(16))) mbox[36];
4546
*/
4647
int mbox_call(unsigned char ch, unsigned int *user_mbox)
4748
{
48-
printf("[1]user_mbox[1]=%p\n",user_mbox[1]);
49+
// printf("[1]user_mbox[1]=%p\n",user_mbox[1]);
50+
// printf("[1]user_mbox[28]=%p\n",user_mbox[28]);
4951
unsigned int r = (((unsigned int)((unsigned long)VA2PA(user_mbox))&~0xF) | (ch&0xF));
5052
/* wait until we can write to the mailbox */
5153
do{asm volatile("nop");}while(*MBOX_STATUS & MBOX_FULL);
@@ -58,7 +60,9 @@ int mbox_call(unsigned char ch, unsigned int *user_mbox)
5860
/* is it a response to our message? */
5961
if(r == *MBOX_READ)
6062
/* is it a valid successful response? */
61-
printf("[2]user_mbox[1]=%p\n",user_mbox[1]);
63+
// printf("[2]user_mbox[1]=%p\n",user_mbox[1]);
64+
// printf("[2]user_mbox[28]=%p\n",user_mbox[28]);
65+
6266
return user_mbox[1]==MBOX_RESPONSE;
6367
}
6468
return 0;

lab6/kernel/src/mmu.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "alloc.h"
55
#include "printf.h"
66
#include "thread.h"
7+
#include "exception.h"
8+
#include "mbox.h"
79

810
void init_page_table(thread_info *thread, uint64_t **table) {
911
*table = (uint64_t *)thread_allocate_page(thread, PAGE_SIZE);
@@ -55,6 +57,7 @@ void update_page_table(thread_info *thread, uint64_t virtual_addr,
5557
}
5658

5759
uint64_t el0_VA2PA(thread_info *thread, uint64_t virtual_addr) {
60+
disable_interrupt();
5861
uint32_t index[4] = {
5962
(virtual_addr >> 39) & 0x1ff, (virtual_addr >> 30) & 0x1ff,
6063
(virtual_addr >> 21) & 0x1ff, (virtual_addr >> 12) & 0x1ff};
@@ -70,7 +73,7 @@ uint64_t el0_VA2PA(thread_info *thread, uint64_t virtual_addr) {
7073
// printf("pgd: %p\n", (uint64_t)table);
7174
for (int level = 0; level < 3; level++) {
7275
if (table[index[level]] == 0) {
73-
printf("Your page table implement unsuccessful\n");
76+
printf("[el0_VA2PA]*Your page table implement unsuccessful*\n");
7477
break;
7578
}
7679
// printf(" [el0_VA2PA]table PA: 0x%p\n", (uint64_t)table[index[level]]);

lab6/kernel/src/shell.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ void command_mailbox()
119119
}
120120
}
121121

122+
unsigned int get_board_revision()
123+
{
124+
// get board revision
125+
mbox[0] = 8*4; // length of the message
126+
mbox[1] = MBOX_REQUEST; // this is a request message
127+
128+
mbox[2] = MBOX_TAG_GETBDVS; // get board revision
129+
mbox[3] = 4; // buffer size
130+
mbox[4] = 4;
131+
mbox[5] = 0; // clear output buffer
132+
mbox[6] = 0;
133+
134+
mbox[7] = MBOX_TAG_LAST;
135+
if (mbox_call(MBOX_CH_PROP,mbox)) {
136+
uart_puts("board revision is: ");
137+
uart_hex(mbox[6]);
138+
uart_hex(mbox[5]);
139+
uart_puts("\n");
140+
}
141+
return mbox[5];
142+
}
143+
122144
void command_test()
123145
{
124146
// test malloc

lab6/kernel/src/thread.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void foo() {
1919

2020
void user_test1() {
2121
const char *argv[] = {"argv_test", "-o", "arg2", 0};
22-
exec("my_test", argv);
22+
exec("mailbox_test", argv);
2323
}
2424

2525
void user_test2() {
@@ -41,11 +41,10 @@ void user_test5() {
4141
exec("vm.img", argv);
4242
}
4343

44-
void thread_test1() { // thread test
44+
void thread_test1() { // mailbox test
4545
thread_info *idle_t = thread_create(0);
4646
asm volatile("msr tpidr_el1, %0\n" ::"r"((uint64_t)idle_t));
4747
thread_create(user_test1);
48-
thread_create(user_test2);
4948
idle();
5049
}
5150

@@ -143,7 +142,7 @@ void idle() {
143142
}
144143

145144
void exit() {
146-
printf("[exit]\n");
145+
// printf("[exit]\n");
147146
thread_info *cur = get_current();
148147
// thread_free_page(cur);
149148
cur->status = THREAD_DEAD;
@@ -177,22 +176,24 @@ void kill_zombies() {
177176
}
178177

179178
void exec(const char *program_name, const char **argv) {
180-
printf("[exec]\n");
179+
// printf("[exec]\n");
181180
thread_info *cur = get_current();
182181
if (cur->user_program_base == 0) {
183182
cur->user_program_base = thread_allocate_page(cur, USER_PROGRAM_SIZE);
184183
cur->user_stack_base = thread_allocate_page(cur, STACK_SIZE);
185184
init_page_table(cur, &(cur->pgd));
186185
}
187-
printf("cur_pgd: 0x%p\n", (uint64_t)(cur->pgd));
188-
printf("user program base: 0x%p\n", cur->user_program_base);
189-
printf("user stack base: 0x%p\n", cur->user_stack_base);
186+
// printf("cur_pgd: 0x%p\n", (uint64_t)(cur->pgd));
187+
// printf("user program base: 0x%p\n", cur->user_program_base);
188+
// printf("user stack base: 0x%p\n", cur->user_stack_base);
190189

191190
cur->user_program_size =
192191
cpio_load_user_program(program_name, cur->user_program_base);
193192
for (uint64_t size = 0; size < cur->user_program_size; size += PAGE_SIZE) {
194193
uint64_t virtual_addr = USER_PROGRAM_BASE + size;
195194
uint64_t physical_addr = VA2PA(cur->user_program_base + size);
195+
if(size==0)
196+
printf("[first_map]va:%p map to pa:%p\n",virtual_addr,physical_addr);
196197
update_page_table(cur, virtual_addr, physical_addr, 0b101);
197198
}
198199

@@ -202,7 +203,13 @@ void exec(const char *program_name, const char **argv) {
202203
// printf("identity_page=%p\n",identity_page);
203204
update_page_table(cur, identity_page , identity_page, 0b101);
204205
}
205-
206+
el0_VA2PA(cur,USER_PROGRAM_BASE);
207+
el0_VA2PA(cur,0x3f000000);
208+
el0_VA2PA(cur,0x3f000000-1);
209+
el0_VA2PA(cur,0x3c25e76c);
210+
el0_VA2PA(cur,0x3c100000);
211+
el0_VA2PA(cur,0x3c000000);
212+
el0_VA2PA(cur,0x3c000000-1);
206213
uint64_t virtual_addr = USER_STACK_BASE;
207214
uint64_t physical_addr = VA2PA(cur->user_stack_base);
208215
update_page_table(cur, virtual_addr, physical_addr, 0b110);

lab6/user/src/argv_test.c

Lines changed: 0 additions & 17 deletions
This file was deleted.

lab6/user/src/mailbox_test.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "start.h"
2+
3+
int main() {
4+
unsigned int __attribute__((aligned(16))) mbox[8];
5+
6+
// get serail number
7+
mbox[0] = 8*4; // length of the message
8+
mbox[1] = MBOX_REQUEST; // this is a request message
9+
10+
mbox[2] = MBOX_TAG_GETSERIAL; // get serial number command
11+
mbox[3] = 8; // buffer size
12+
mbox[4] = 8;
13+
mbox[5] = 0; // clear output buffer
14+
mbox[6] = 0;
15+
16+
mbox[7] = MBOX_TAG_LAST;
17+
print_s("\n");
18+
if (mbox_call(MBOX_CH_PROP,mbox)) {
19+
print_s("serial number is: ");
20+
print_h(mbox[6]);
21+
print_h(mbox[5]);
22+
print_s("\n");
23+
}
24+
// get board revision
25+
mbox[0] = 8*4; // length of the message
26+
mbox[1] = MBOX_REQUEST; // this is a request message
27+
28+
mbox[2] = MBOX_TAG_GETBDVS; // get board revision
29+
mbox[3] = 4; // buffer size
30+
mbox[4] = 4;
31+
mbox[5] = 0; // clear output buffer
32+
mbox[6] = 0;
33+
34+
mbox[7] = MBOX_TAG_LAST;
35+
if (mbox_call(MBOX_CH_PROP,mbox)) {
36+
print_s("board revision is: ");
37+
print_h(mbox[6]);
38+
print_h(mbox[5]);
39+
print_s("\n");
40+
}
41+
42+
// get arm memory
43+
mbox[0] = 8*4; // length of the message
44+
mbox[1] = MBOX_REQUEST; // this is a request message
45+
46+
mbox[2] = MBOX_TAG_GETARMMEM; // get arm memory info
47+
mbox[3] = 8; // buffer size
48+
mbox[4] = 8;
49+
mbox[5] = 0; // clear output buffer
50+
mbox[6] = 0;
51+
52+
mbox[7] = MBOX_TAG_LAST;
53+
if (mbox_call(MBOX_CH_PROP,mbox)) {
54+
print_s("arm base addr: ");
55+
print_h(mbox[5]);
56+
print_s("\n");
57+
print_s("arm addr size: ");
58+
print_h(mbox[6]);
59+
print_s("\n");
60+
}
61+
// while(1);
62+
return 0;
63+
}

0 commit comments

Comments
 (0)