Refactor system_breakpoint_handler

Modifies system_breakpoint_handler so that all fox32 registers are saved
on the stack in a predictable order, and they are printed using a loop.
Also comments out two seemingly unneccessary additions in the monitor's
hex printing functions.
This commit is contained in:
mebibytedraco 2023-12-22 15:05:02 -05:00
parent bb3d709ad6
commit b1aa77aa70
2 changed files with 82 additions and 483 deletions

View File

@ -41,12 +41,14 @@ system_page_fault_handler:
system_page_fault_str: data.str "Page fault at virtual address r1" data.8 10 data.8 0
; called upon execution of a `brk` instruction
; ensure the stack has at least 256 bytes of free space before triggering this exception!!
; this code is extremely ugly, but it works :P
; ensure the stack has at least 128 bytes of free space before triggering this exception!!
system_breakpoint_handler:
add rsp, 4
; push all registers once to save them
push rfp
push resp
push rsp
push r31
push r30
push r29
@ -80,470 +82,60 @@ system_breakpoint_handler:
push r1
push r0
; then push all registers again so they can be popped one by one to print to the monitor
push r31
push r30
push r29
push r28
push r27
push r26
push r25
push r24
push r23
push r22
push r21
push r20
push r19
push r18
push r17
push r16
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push r7
push r6
push r5
push r4
push r3
push r2
push r1
push r0
; modify the saved rsp value to reflect the value of rsp before the
; interrupt occured
; resp (4) + rfp (4) + flags (1) + return address (4) = 13 bytes
add [rsp+128], 13
; print breakpoint message
mov r0, system_breakpoint_str
call debug_print
call print_string_to_monitor
; r0
mov r0, system_breakpoint_r0_str
; print the display containing all of the registers
; r1 - used to store a pointer to the current string
; r2 - stores the current address on the stack
; r3 - loop counter
mov r1, system_breakpoint_r0_str
mov r2, rsp
mov r3, 0
system_breakpoint_print_loop:
; print the register label
mov r0, r1
call print_string_to_monitor
pop r0
; print the register value
mov r0, [r2]
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r1
mov r0, system_breakpoint_r1_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r2
mov r0, system_breakpoint_r2_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r3
mov r0, system_breakpoint_r3_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
; adjust string pointer, stack address, and loop counter
add r1, SYSTEM_BREAKPOINT_R_STR_SIZE
inc r2, 4
inc r3
; decide whether to print a separator or a newline by checking if the loop
; counter is a multiple of 4
mov r0, r3
and r0, 0x03
ifnz jmp system_breakpoint_print_sep
system_breakpoint_print_newline:
mov r0, 10
call print_character_to_monitor
; ---
; r4
mov r0, system_breakpoint_r4_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
jmp system_breakpoint_print_last_char
system_breakpoint_print_sep:
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
system_breakpoint_print_last_char:
call print_character_to_monitor
; r5
mov r0, system_breakpoint_r5_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r6
mov r0, system_breakpoint_r6_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r7
mov r0, system_breakpoint_r7_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r8
mov r0, system_breakpoint_r8_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r9
mov r0, system_breakpoint_r9_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r10
mov r0, system_breakpoint_r10_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r11
mov r0, system_breakpoint_r11_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r12
mov r0, system_breakpoint_r12_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r13
mov r0, system_breakpoint_r13_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r14
mov r0, system_breakpoint_r14_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r15
mov r0, system_breakpoint_r15_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r16
mov r0, system_breakpoint_r16_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r17
mov r0, system_breakpoint_r17_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r18
mov r0, system_breakpoint_r18_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r19
mov r0, system_breakpoint_r19_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r20
mov r0, system_breakpoint_r20_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r21
mov r0, system_breakpoint_r21_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r22
mov r0, system_breakpoint_r22_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r23
mov r0, system_breakpoint_r23_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r24
mov r0, system_breakpoint_r24_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r25
mov r0, system_breakpoint_r25_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r26
mov r0, system_breakpoint_r26_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r27
mov r0, system_breakpoint_r27_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; r28
mov r0, system_breakpoint_r28_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r29
mov r0, system_breakpoint_r29_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r30
mov r0, system_breakpoint_r30_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; r31
mov r0, system_breakpoint_r31_str
call print_string_to_monitor
pop r0
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
; rsp
mov r0, system_breakpoint_rsp_str
call print_string_to_monitor
mov r0, rsp
add r0, 133 ; account for the registers pushed above, and for the int calling convention
call print_hex_word_to_monitor
mov r0, ' '
call print_character_to_monitor
mov r0, '|'
call print_character_to_monitor
mov r0, ' '
call print_character_to_monitor
; rip
; loop again if not on last register
cmp r3, 35
iflt jmp system_breakpoint_print_loop
; print rip
mov r0, system_breakpoint_rip_str
call print_string_to_monitor
mov r0, rsp
add r0, 129 ; read instruction pointer from the stack
mov r0, [r0]
mov r0, [r2+1]
call print_hex_word_to_monitor
; ---
mov r0, 10
call print_character_to_monitor
; ---
call invoke_monitor
@ -579,39 +171,46 @@ system_breakpoint_handler:
pop r29
pop r30
pop r31
; don't restore rsp and resp. not sure whether restoring a potentially
; modified resp would break things, but changing rsp definitely would.
add rsp, 8
pop rfp
reti
system_breakpoint_str: data.str "Breakpoint reached!" data.8 10 data.8 0
system_breakpoint_r0_str: data.strz "r0: "
system_breakpoint_r1_str: data.strz "r1: "
system_breakpoint_r2_str: data.strz "r2: "
system_breakpoint_r3_str: data.strz "r3: "
system_breakpoint_r4_str: data.strz "r4: "
system_breakpoint_r5_str: data.strz "r5: "
system_breakpoint_r6_str: data.strz "r6: "
system_breakpoint_r7_str: data.strz "r7: "
system_breakpoint_r8_str: data.strz "r8: "
system_breakpoint_r9_str: data.strz "r9: "
system_breakpoint_r10_str: data.strz "r10: "
system_breakpoint_r11_str: data.strz "r11: "
system_breakpoint_r12_str: data.strz "r12: "
system_breakpoint_r13_str: data.strz "r13: "
system_breakpoint_r14_str: data.strz "r14: "
system_breakpoint_r15_str: data.strz "r15: "
system_breakpoint_r16_str: data.strz "r16: "
system_breakpoint_r17_str: data.strz "r17: "
system_breakpoint_r18_str: data.strz "r18: "
system_breakpoint_r19_str: data.strz "r19: "
system_breakpoint_r20_str: data.strz "r20: "
system_breakpoint_r21_str: data.strz "r21: "
system_breakpoint_r22_str: data.strz "r22: "
system_breakpoint_r23_str: data.strz "r23: "
system_breakpoint_r24_str: data.strz "r24: "
system_breakpoint_r25_str: data.strz "r25: "
system_breakpoint_r26_str: data.strz "r26: "
system_breakpoint_r27_str: data.strz "r27: "
system_breakpoint_r28_str: data.strz "r28: "
system_breakpoint_r29_str: data.strz "r29: "
system_breakpoint_r30_str: data.strz "r30: "
system_breakpoint_r31_str: data.strz "r31: "
system_breakpoint_rsp_str: data.strz "rsp: "
system_breakpoint_rip_str: data.strz "rip: "
const SYSTEM_BREAKPOINT_R_STR_SIZE: 7
system_breakpoint_r0_str: data.strz "r0: "
system_breakpoint_r1_str: data.strz "r1: "
system_breakpoint_r2_str: data.strz "r2: "
system_breakpoint_r3_str: data.strz "r3: "
system_breakpoint_r4_str: data.strz "r4: "
system_breakpoint_r5_str: data.strz "r5: "
system_breakpoint_r6_str: data.strz "r6: "
system_breakpoint_r7_str: data.strz "r7: "
system_breakpoint_r8_str: data.strz "r8: "
system_breakpoint_r9_str: data.strz "r9: "
system_breakpoint_r10_str: data.strz "r10: "
system_breakpoint_r11_str: data.strz "r11: "
system_breakpoint_r12_str: data.strz "r12: "
system_breakpoint_r13_str: data.strz "r13: "
system_breakpoint_r14_str: data.strz "r14: "
system_breakpoint_r15_str: data.strz "r15: "
system_breakpoint_r16_str: data.strz "r16: "
system_breakpoint_r17_str: data.strz "r17: "
system_breakpoint_r18_str: data.strz "r18: "
system_breakpoint_r19_str: data.strz "r19: "
system_breakpoint_r20_str: data.strz "r20: "
system_breakpoint_r21_str: data.strz "r21: "
system_breakpoint_r22_str: data.strz "r22: "
system_breakpoint_r23_str: data.strz "r23: "
system_breakpoint_r24_str: data.strz "r24: "
system_breakpoint_r25_str: data.strz "r25: "
system_breakpoint_r26_str: data.strz "r26: "
system_breakpoint_r27_str: data.strz "r27: "
system_breakpoint_r28_str: data.strz "r28: "
system_breakpoint_r29_str: data.strz "r29: "
system_breakpoint_r30_str: data.strz "r30: "
system_breakpoint_r31_str: data.strz "r31: "
system_breakpoint_rsp_str: data.strz "rsp: "
system_breakpoint_resp_str: data.strz "resp: "
system_breakpoint_rfp_str: data.strz "rfp: "
system_breakpoint_rip_str: data.strz "rip: "

View File

@ -38,7 +38,7 @@ print_hex_word_to_monitor_loop:
add r12, r11
movz.8 r0, [r12]
call print_character_to_monitor
add r1, r6
;add r1, r6
loop print_hex_word_to_monitor_loop
pop r31
@ -67,7 +67,7 @@ print_hex_byte_to_monitor_loop:
add r12, r11
movz.8 r0, [r12]
call print_character_to_monitor
add r1, r6
;add r1, r6
loop print_hex_byte_to_monitor_loop
pop r31