-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtrap.rs
More file actions
121 lines (110 loc) · 3.51 KB
/
Copy pathtrap.rs
File metadata and controls
121 lines (110 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use aarch64_cpu::registers::{ESR_EL1, FAR_EL1};
use tock_registers::interfaces::Readable;
use super::TrapFrame;
use crate::trap::PageFaultFlags;
core::arch::global_asm!(include_str!("trap.S"));
#[repr(u8)]
#[derive(Debug)]
#[allow(dead_code)]
enum TrapKind {
Synchronous = 0,
Irq = 1,
Fiq = 2,
SError = 3,
}
#[repr(u8)]
#[derive(Debug)]
#[allow(dead_code)]
enum TrapSource {
CurrentSpEl0 = 0,
CurrentSpElx = 1,
LowerAArch64 = 2,
LowerAArch32 = 3,
}
#[unsafe(no_mangle)]
fn invalid_exception(tf: &TrapFrame, kind: TrapKind, source: TrapSource) {
panic!(
"Invalid exception {:?} from {:?}:\n{:#x?}",
kind, source, tf
);
}
#[unsafe(no_mangle)]
fn handle_irq_exception(_tf: &TrapFrame) {
handle_trap!(IRQ, 0);
}
fn handle_instruction_abort(tf: &TrapFrame, iss: u64, is_user: bool) {
let mut access_flags = PageFaultFlags::EXECUTE;
if is_user {
access_flags |= PageFaultFlags::USER;
}
let vaddr = va!(FAR_EL1.get() as usize);
// Only handle Translation fault and Permission fault
if !matches!(iss & 0b111100, 0b0100 | 0b1100) // IFSC or DFSC bits
|| !handle_trap!(PAGE_FAULT, vaddr, access_flags, is_user)
{
panic!(
"Unhandled {} Instruction Abort @ {:#x}, fault_vaddr={:#x}, ESR={:#x} ({:?}):\n{:#x?}",
if is_user { "EL0" } else { "EL1" },
tf.elr,
vaddr,
ESR_EL1.get(),
access_flags,
tf,
);
}
}
fn handle_data_abort(tf: &TrapFrame, iss: u64, is_user: bool) {
let wnr = (iss & (1 << 6)) != 0; // WnR: Write not Read
let cm = (iss & (1 << 8)) != 0; // CM: Cache maintenance
let mut access_flags = if wnr & !cm {
PageFaultFlags::WRITE
} else {
PageFaultFlags::READ
};
if is_user {
access_flags |= PageFaultFlags::USER;
}
let vaddr = va!(FAR_EL1.get() as usize);
// Only handle Translation fault and Permission fault
if !matches!(iss & 0b111100, 0b0100 | 0b1100) // IFSC or DFSC bits
|| !handle_trap!(PAGE_FAULT, vaddr, access_flags, is_user)
{
panic!(
"Unhandled {} Data Abort @ {:#x}, fault_vaddr={:#x}, ESR={:#x} ({:?}):\n{:#x?}",
if is_user { "EL0" } else { "EL1" },
tf.elr,
vaddr,
ESR_EL1.get(),
access_flags,
tf,
);
}
}
#[unsafe(no_mangle)]
fn handle_sync_exception(tf: &mut TrapFrame) {
let esr = ESR_EL1.extract();
let iss = esr.read(ESR_EL1::ISS);
match esr.read_as_enum(ESR_EL1::EC) {
#[cfg(feature = "uspace")]
Some(ESR_EL1::EC::Value::SVC64) => {
tf.r[0] = crate::trap::handle_syscall(tf, tf.r[8] as usize) as u64;
}
Some(ESR_EL1::EC::Value::InstrAbortLowerEL) => handle_instruction_abort(tf, iss, true),
Some(ESR_EL1::EC::Value::InstrAbortCurrentEL) => handle_instruction_abort(tf, iss, false),
Some(ESR_EL1::EC::Value::DataAbortLowerEL) => handle_data_abort(tf, iss, true),
Some(ESR_EL1::EC::Value::DataAbortCurrentEL) => handle_data_abort(tf, iss, false),
Some(ESR_EL1::EC::Value::Brk64) => {
debug!("BRK #{:#x} @ {:#x} ", iss, tf.elr);
tf.elr += 4;
}
_ => {
panic!(
"Unhandled synchronous exception @ {:#x}: ESR={:#x} (EC {:#08b}, ISS {:#x})",
tf.elr,
esr.get(),
esr.read(ESR_EL1::EC),
esr.read(ESR_EL1::ISS),
);
}
}
}