Skip to content

Commit dccb5a8

Browse files
authored
Merge pull request #643 from ChinYikMing/fix-syscall-exit
Fix hart halt when non-SDL programs exit via syscall 93
2 parents 7c1b40c + 6dda367 commit dccb5a8

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

src/emulate.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,19 +1444,32 @@ void ecall_handler(riscv_t *rv)
14441444
syscall_handler(rv);
14451445
#elif RV32_HAS(SYSTEM)
14461446
if (rv->priv_mode == RV_PRIV_U_MODE) {
1447-
switch (rv_get_reg(
1448-
rv,
1449-
rv_reg_a7)) { /* trap guestOS's SDL-oriented application syscall */
1447+
uint32_t reg_a7 = rv_get_reg(rv, rv_reg_a7);
1448+
switch (reg_a7) { /* trap guestOS's SDL-oriented application syscall */
14501449
case 0xBEEF:
14511450
case 0xC0DE:
14521451
case 0xFEED:
14531452
case 0xBABE:
14541453
case 0xD00D:
1455-
case 93:
14561454
syscall_handler(rv);
14571455
rv->PC += 4;
14581456
break;
14591457
default:
1458+
#if RV32_HAS(SDL) && RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
1459+
/*
1460+
* The guestOS may repeatedly open and close the SDL window,
1461+
* and the user could close the application using the application’s
1462+
* built-in exit function. Need to trap the built-in exit and
1463+
* ensure the SDL window and SDL mixer are destroyed properly.
1464+
*/
1465+
{
1466+
extern void sdl_video_audio_cleanup();
1467+
if (unlikely(PRIV(rv)->running_sdl && reg_a7 == 93)) {
1468+
sdl_video_audio_cleanup();
1469+
PRIV(rv)->running_sdl = false;
1470+
}
1471+
}
1472+
#endif
14601473
SET_CAUSE_AND_TVAL_THEN_TRAP(rv, ECALL_U, 0);
14611474
break;
14621475
}

src/riscv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@ typedef struct {
568568
bool on_exit;
569569
#endif
570570

571+
#if RV32_HAS(SDL) && RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
572+
/* flag to determine if running SDL program in guestOS */
573+
bool running_sdl;
574+
#endif /* SDL */
575+
571576
/* SBI timer */
572577
uint64_t timer;
573578
} vm_attr_t;

src/syscall.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,6 @@ static void syscall_write(riscv_t *rv)
134134

135135
static void syscall_exit(riscv_t *rv)
136136
{
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-
149137
/* simply halt cpu and save exit code.
150138
* the application decides the usage of exit code
151139
*/

src/syscall_sdl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ void syscall_setup_queue(riscv_t *rv)
393393
*/
394394
event_queue.base = event_queue.end = 0;
395395
submission_queue.base = submission_queue.start = 0;
396+
PRIV(rv)->running_sdl = true;
396397
#endif
397398

398399
/* setup_queue(base, capacity, event_count) */

0 commit comments

Comments
 (0)