1414 read csr_mtimecmph prev_proc "csr_mtimecmph"
1515 read csr_mtimeh prev_proc "csr_mtimeh"
1616 read csr_satp prev_proc "csr_satp"
17+ read csr_stimecmp prev_proc "csr_stimecmp"
18+ read csr_stimecmph prev_proc "csr_stimecmph"
1719 read effective_privilege_mode prev_proc "effective_privilege_mode"
1820 read icache_payload prev_proc "icache_payload"
1921 read icache_ram prev_proc "icache_ram"
9395
9496#% set labels.next_tick = 14
9597
96- #% set labels.end_instruction_with_rd_and_poll_interrupts = 148
98+ #% set labels.end_instruction_with_rd_and_poll_interrupts = 157
9799#% set labels.end_instruction_with_rd_and_fire_interrupts = _labels_vals|last + 1
98100#% set labels.end_instruction_with_rd = _labels_vals|last + 1
99101#% set labels.end_instruction = _labels_vals|last + 2
@@ -163,10 +165,21 @@ poll_interrupts:
163165 op or $mip.mtip $mip.mtip $high_gt
164166 op shl $mip.mtip $mip.mtip 7
165167
168+ # supervisor timer interrupt
169+ op greaterThan $high_gt csr_mtimeh csr_stimecmph
170+ op equal $high_eq csr_mtimeh csr_stimecmph
171+ op greaterThanEq $low_ge csr_mtime csr_stimecmp
172+
173+ op and $mip.stip $high_eq $low_ge
174+ op or $mip.stip $mip.stip $high_gt
175+ op shl $mip.stip $mip.stip 5
176+
166177 # poll -
167178 # mtip -
168- op and csr_mip csr_mip 0b01111111111111111111111101111111
179+ # stip -
180+ op and csr_mip csr_mip 0b01111111111111111111111101011111
169181 op or csr_mip csr_mip $mip.mtip
182+ op or csr_mip csr_mip $mip.stip
170183
171184# check if interrupts should fire
172185# this runs at the start of the first worker each tick, if certain CSRs are modified, and after an xRET instruction
@@ -2283,6 +2296,7 @@ modify_csr:
22832296 jump ILLEGAL_OP equal $required_privilege 0b10
22842297
22852298 # look up the CSR labels
2299+ # NOTE: this value in _address is later used by check_xtimecmp
22862300 # csr_labels = csr_read * 1000 + csr_write
22872301 set _address imm
22882302 op add ret2 @counter 1
@@ -2370,7 +2384,7 @@ csr_read_sstatus:
23702384csr_write_sstatus:
23712385 # insert new writable fields
23722386 op and csr_mstatus csr_mstatus 0b11111111111100111111111011011101
2373- op and result result 0b00000000000011000000000100100010
2387+ op and result result 0b00000000000011000000000100100010
23742388 op or csr_mstatus csr_mstatus result
23752389
23762390 jump csr_xstatus always
@@ -2382,9 +2396,9 @@ csr_read_mstatus:
23822396
23832397csr_write_mstatus:
23842398 # clear WPRI and read-only zero fields
2385- # SD/XS/FS/VS - ---- --
2386- # WPRI -------- - - -
2387- # UBE -
2399+ # SD/XS/FS/VS - ---- --
2400+ # WPRI -------- - - -
2401+ # UBE -
23882402 op and csr_mstatus result 0b00000000011111100001100110101010
23892403
23902404 # if MPP was set to 10 (reserved), set it to 0
@@ -2409,9 +2423,9 @@ csr_read_sip:
24092423
24102424csr_write_sip:
24112425 # insert new writable fields
2412- # SSIP -
2413- op and csr_mip csr_mip 0b11111111111111111111111111111101
2414- op and result result 0b00000000000000000000000000000010
2426+ # SSIP -
2427+ op and csr_mip csr_mip 0b11111111111111111111111111111101
2428+ op and result result 0b00000000000000000000000000000010
24152429 op or csr_mip csr_mip result
24162430
24172431 jump end_instruction_with_rd_and_fire_interrupts always
@@ -2424,11 +2438,10 @@ csr_read_mip:
24242438
24252439csr_write_mip:
24262440 # insert new writable fields
2427- # SEIP -
2428- # STIP -
2429- # SSIP -
2430- op and csr_mip csr_mip 0b11111111111111111111110111011101
2431- op and result result 0b00000000000000000000001000100010
2441+ # SEIP -
2442+ # SSIP -
2443+ op and csr_mip csr_mip 0b11111111111111111111110111011101
2444+ op and result result 0b00000000000000000000001000000010
24322445 op or csr_mip csr_mip result
24332446
24342447 jump end_instruction_with_rd_and_fire_interrupts always
@@ -2445,11 +2458,11 @@ csr_read_sie:
24452458
24462459csr_write_sie:
24472460 # insert new writable fields
2448- # SEIE -
2449- # STIE -
2450- # SSIE -
2451- op and csr_mie csr_mie 0b11111111111111111111110111011101
2452- op and result result 0b00000000000000000000001000100010
2461+ # SEIE -
2462+ # STIE -
2463+ # SSIE -
2464+ op and csr_mie csr_mie 0b11111111111111111111110111011101
2465+ op and result result 0b00000000000000000000001000100010
24532466 op or csr_mie csr_mie result
24542467
24552468 jump end_instruction_with_rd_and_fire_interrupts always
@@ -2461,11 +2474,11 @@ csr_read_mie:
24612474
24622475csr_write_mie:
24632476 # get new writable fields
2464- # MEIE -
2465- # SEIE -
2466- # MTIE -
2467- # STIE -
2468- # SSIE -
2477+ # MEIE -
2478+ # SEIE -
2479+ # MTIE -
2480+ # STIE -
2481+ # SSIE -
24692482 op and csr_mie result 0b00000000000000000000101010100010
24702483 jump end_instruction_with_rd_and_fire_interrupts always
24712484
@@ -2489,6 +2502,30 @@ csr_write_satp:
24892502 # TODO: this may not be strictly required by the spec
24902503 jump end_instruction_with_rd_and_invalidate_icache always
24912504
2505+ csr_read_stimecmp:
2506+ #% do declare_locals(modify_csr_locals)
2507+ set rd csr_stimecmp
2508+ jump check_stimecmp always
2509+
2510+ csr_write_stimecmp:
2511+ set csr_stimecmp result
2512+ jump end_instruction_with_rd_and_poll_interrupts always
2513+
2514+ csr_read_stimecmph:
2515+ #% do declare_locals(modify_csr_locals)
2516+ set rd csr_stimecmph
2517+ # continue into check_stimecmp
2518+
2519+ check_stimecmp:
2520+ # stimecmp is controlled by the TM bit in mcounteren
2521+ # so we set _address to 1 and use check_xcounteren to check the TM bit
2522+ set _address 1
2523+ jump check_xcounteren always
2524+
2525+ csr_write_stimecmph:
2526+ set csr_stimecmph result
2527+ jump end_instruction_with_rd_and_poll_interrupts always
2528+
24922529csr_read_cycle:
24932530#% do declare_locals(modify_csr_locals)
24942531 read rd {{CSRS}} "{{ 'mcycle'|csr }}"
@@ -2549,7 +2586,8 @@ check_xcounteren:
25492586check_xcounteren__s_mode:
25502587
25512588 # trap if the bit in xcounteren corresponding to the CSR being read is clear
2552- op and $bit imm 31
2589+ # we use _address instead of imm here to allow check_stimecmp to change it without messing up debug info
2590+ op and $bit _address 31
25532591 op shr $bit $xcounteren $bit
25542592 op and $bit $bit 0b1
25552593 jump ILLEGAL_OP equal $bit 0
@@ -2706,33 +2744,37 @@ jump decode_BRANCH always
27062744jump decode_JALR always
27072745jump decode_JAL always
27082746jump decode_SYSTEM always
2747+ jump csr_read_csrs always
27092748jump csr_read_cycle always
2710- jump csr_read_time always
2711- jump csr_read_minstret always
2712- jump csr_read_hpmcounter always
2713- jump csr_read_zero always
27142749jump csr_read_cycleh always
2715- jump csr_read_timeh always
2716- jump csr_read_minstreth always
2717- jump csr_read_sstatus always
2718- jump csr_read_sie always
2719- jump csr_read_csrs always
2720- jump csr_read_sip always
2721- jump csr_read_satp always
2722- jump csr_read_mstatus always
2750+ jump csr_read_hpmcounter always
27232751jump csr_read_mideleg always
27242752jump csr_read_mie always
2753+ jump csr_read_minstret always
2754+ jump csr_read_minstreth always
27252755jump csr_read_mip always
2726- jump csr_write_sstatus always
2727- jump csr_write_sie always
2728- jump csr_write_csrs_align_4B always
2756+ jump csr_read_mstatus always
2757+ jump csr_read_satp always
2758+ jump csr_read_sie always
2759+ jump csr_read_sip always
2760+ jump csr_read_sstatus always
2761+ jump csr_read_stimecmp always
2762+ jump csr_read_stimecmph always
2763+ jump csr_read_time always
2764+ jump csr_read_timeh always
2765+ jump csr_read_zero always
27292766jump csr_write_csrs always
2730- jump csr_write_readonly always
2731- jump csr_write_sip always
2732- jump csr_write_satp always
2733- jump csr_write_mstatus always
2767+ jump csr_write_csrs_align_4B always
27342768jump csr_write_mie always
2735- jump csr_write_mip always
27362769jump csr_write_minstret always
27372770jump csr_write_minstreth always
2771+ jump csr_write_mip always
2772+ jump csr_write_mstatus always
2773+ jump csr_write_readonly always
2774+ jump csr_write_satp always
2775+ jump csr_write_sie always
2776+ jump csr_write_sip always
2777+ jump csr_write_sstatus always
2778+ jump csr_write_stimecmp always
2779+ jump csr_write_stimecmph always
27382780# {% endraw %}
0 commit comments