Skip to content

Commit a8b202c

Browse files
authored
Merge pull request #551 from ChinYikMing/trap-guestOS-SDL-app
Trap guestOS to run SDL-oriented applications
2 parents 8d47e5b + f20b959 commit a8b202c

File tree

6 files changed

+232
-28
lines changed

6 files changed

+232
-28
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ $ make ENABLE_SYSTEM=1
8080
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path>
8181
```
8282

83+
Build with a larger INITRD_SIZE (e.g., 64 MiB) to run SDL-oriented application because the default 8 MiB is insufficient for SDL-oriented application artifacts:
84+
```shell
85+
$ make system ENABLE_SYSTEM=1 ENABLE_SDL=1 INITRD_SIZE=64
86+
```
87+
Once login the guestOS, run `doom-riscv` or `quake` or `smolnes`. To terminate SDL-oriented applications, use the built-in exit utility, ctrl-c or the SDL window close button(X).
88+
8389
#### Build Linux image
8490
An automated build script is provided to compile the RISC-V cross-compiler, Busybox, and Linux kernel from source. Please note that it only supports the Linux host environment. It can be found at tools/build-linux-image.sh.
8591
```

src/devices/uart.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <unistd.h>
1313

1414
#include "uart.h"
15-
1615
/* Emulate 8250 (plain, without loopback mode support) */
1716

1817
#define U8250_INTR_THRE 1
@@ -66,10 +65,22 @@ static uint8_t u8250_handle_in(u8250_state_t *uart)
6665
if (value == 1) { /* start of heading (Ctrl-a) */
6766
if (getchar() == 120) { /* keyboard x */
6867
printf("\n"); /* end emulator with newline */
69-
exit(0);
68+
exit(EXIT_SUCCESS);
7069
}
7170
}
7271

72+
#if RV32_HAS(SDL) && RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
73+
/*
74+
* The guestOS may repeatedly open and close the SDL window,
75+
* and the user could close the application by pressing the ctrl-c key.
76+
* Need to trap the ctrl-c key and ensure the SDL window and
77+
* SDL mixer are destroyed properly.
78+
*/
79+
extern void sdl_video_audio_cleanup();
80+
if (value == 3) /* ctrl-c */
81+
sdl_video_audio_cleanup();
82+
#endif
83+
7384
return value;
7485
}
7586

src/emulate.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,22 @@ void ecall_handler(riscv_t *rv)
12991299
syscall_handler(rv);
13001300
#elif RV32_HAS(SYSTEM)
13011301
if (rv->priv_mode == RV_PRIV_U_MODE) {
1302-
SET_CAUSE_AND_TVAL_THEN_TRAP(rv, ECALL_U, 0);
1302+
switch (rv_get_reg(
1303+
rv,
1304+
rv_reg_a7)) { /* trap guestOS's SDL-oriented application syscall */
1305+
case 0xBEEF:
1306+
case 0xC0DE:
1307+
case 0xFEED:
1308+
case 0xBABE:
1309+
case 0xD00D:
1310+
case 93:
1311+
syscall_handler(rv);
1312+
rv->PC += 4;
1313+
break;
1314+
default:
1315+
SET_CAUSE_AND_TVAL_THEN_TRAP(rv, ECALL_U, 0);
1316+
break;
1317+
}
13031318
} else if (rv->priv_mode ==
13041319
RV_PRIV_S_MODE) { /* trap to SBI syscall handler */
13051320
rv->PC += 4;

src/syscall.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,20 @@ static void syscall_write(riscv_t *rv)
132132
rv_set_reg(rv, rv_reg_a0, -1);
133133
}
134134

135-
136135
static void syscall_exit(riscv_t *rv)
137136
{
137+
#if RV32_HAS(SDL) && RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
138+
/*
139+
* The guestOS may repeatedly open and close the SDL window,
140+
* and the user could close the application using the application’s
141+
* built-in exit function. Need to trap the built-in exit and
142+
* ensure the SDL window and SDL mixer are destroyed properly.
143+
*/
144+
extern void sdl_video_audio_cleanup();
145+
sdl_video_audio_cleanup();
146+
return;
147+
#endif
148+
138149
/* simply halt cpu and save exit code.
139150
* the application decides the usage of exit code
140151
*/

0 commit comments

Comments
 (0)