Commit c9510ce
committed
Handle signals properly
Page faults trigger a trap, which is handled by do_page_fault(). This
function calls lock_mm_and_find_vma() to locate and validate the virtual
memory area (VMA), returning the VMA if valid, or NULL otherwise.
Typically, attempts to read or write to a NULL VMA result in a NULL
return. If the VMA is invalid, bad_area_nosemaphore() is invoked, which
checks whether the fault originated in kernel or user space.
For user-space faults, a SIGSEGV signal is sent to the user process via
do_trap(), which determines if the signal should be ignored or blocked,
and if not, adds it to the task's pending signal list. Kernel-space
faults cause the kernel to crash via die_kernel_fault().
Before returning to user space (via the resume_userspace label), pending
work (indicated by the _TIF_WORK_MASK mask) is processed by
do_work_pending(). Signals are handled by do_signal(), which in turn
calls handle_signal(). handle_signal() creates a signal handler frame
that will be jumped to upon returning to user space. This frame creation
process might modifies the Control and Status Register (CSR) SEPC.
If there are a signal pending, the SEPC CSR overwritten the original
trap/fault PC. This caused an assertion failure in get_ppn_and_offset()
when running the vi program, reported in [1].
To address this, a variable last_csr_sepc was introduced to store the
original SEPC CSR value before entering the trap path. After returning
to user space, last_csr_sepc is compared with the current SEPC CSR
value. If they differ, the fault ld/st instruction returns early and
jumps to the signal handler frame.
This commit prevents emulator crashes when the guest OS accesses invalid
memory. Consequently, reads or writes to a NULL value now correctly
result in a segmentation fault. In addition, two user-space programs:
mem_null_read and mem_null_write are bundled into the rootfs for
verification.
Original behaviour
1. $ make system ENABLE_SYSTEM=1 -j$(nproc)
2. $ mem_null_read # Emulator crashes
3. $ mem_null_write # Emulator crashes
4. $ vi # Emulator crashes
Patch Reproduce / Testing procedure:
1. $ make system ENABLE_SYSTEM=1 -j$(nproc)
2. $ mem_null_read # NULL read causes SIGSEGV without crashing
3. $ mem_null_write # NULL write causes SIGSEGV without crashing
4. $ vi # w/o filename causes SIGSEGV without crashing
[1] sysprog21#5081 parent f7fd15b commit c9510ce
3 files changed
+51
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| |||
379 | 380 | | |
380 | 381 | | |
381 | 382 | | |
382 | | - | |
383 | | - | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
384 | 389 | | |
385 | 390 | | |
386 | 391 | | |
| |||
1179 | 1184 | | |
1180 | 1185 | | |
1181 | 1186 | | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
1182 | 1190 | | |
1183 | 1191 | | |
1184 | 1192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
204 | 210 | | |
205 | 211 | | |
206 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
28 | 44 | | |
29 | 45 | | |
30 | 46 | | |
| |||
269 | 285 | | |
270 | 286 | | |
271 | 287 | | |
| 288 | + | |
272 | 289 | | |
273 | 290 | | |
274 | 291 | | |
| |||
297 | 314 | | |
298 | 315 | | |
299 | 316 | | |
300 | | - | |
| 317 | + | |
| 318 | + | |
301 | 319 | | |
| 320 | + | |
302 | 321 | | |
303 | 322 | | |
304 | 323 | | |
| |||
323 | 342 | | |
324 | 343 | | |
325 | 344 | | |
326 | | - | |
| 345 | + | |
| 346 | + | |
327 | 347 | | |
| 348 | + | |
328 | 349 | | |
329 | 350 | | |
330 | 351 | | |
| |||
338 | 359 | | |
339 | 360 | | |
340 | 361 | | |
341 | | - | |
| 362 | + | |
| 363 | + | |
342 | 364 | | |
| 365 | + | |
343 | 366 | | |
344 | 367 | | |
345 | 368 | | |
| |||
364 | 387 | | |
365 | 388 | | |
366 | 389 | | |
367 | | - | |
| 390 | + | |
| 391 | + | |
368 | 392 | | |
| 393 | + | |
369 | 394 | | |
370 | 395 | | |
371 | 396 | | |
| |||
390 | 415 | | |
391 | 416 | | |
392 | 417 | | |
393 | | - | |
| 418 | + | |
| 419 | + | |
394 | 420 | | |
| 421 | + | |
395 | 422 | | |
396 | 423 | | |
397 | 424 | | |
| |||
405 | 432 | | |
406 | 433 | | |
407 | 434 | | |
408 | | - | |
| 435 | + | |
| 436 | + | |
409 | 437 | | |
| 438 | + | |
410 | 439 | | |
411 | 440 | | |
412 | 441 | | |
| |||
0 commit comments