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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! The exception module contains all the exception kinds and the function to handle exceptions.

use crate::{
    cpu::{Cpu, Mode},
    csr::*,
};

/// All the exception kinds.
#[derive(Debug)]
pub enum Exception {
    InstructionAddressMisaligned(String),
    InstructionAccessFault,
    IllegalInstruction(String),
    Breakpoint,
    LoadAddressMisaligned,
    LoadAccessFault,
    StoreAMOAddressMisaligned,
    StoreAMOAccessFault,
    EnvironmentCallFromUMode,
    EnvironmentCallFromSMode,
    EnvironmentCallFromMMode,
    InstructionPageFault,
    LoadPageFault,
    StoreAMOPageFault,
}

impl Exception {
    /// Update CSRs and the program counter depending on an exception.
    pub fn take_trap(&self, cpu: &mut Cpu) -> Result<(), Exception> {
        let exception_code;
        let exception_pc = (cpu.pc as i64) - 4;

        match self {
            Exception::InstructionAddressMisaligned(_s) => {
                exception_code = 0;
            }
            Exception::InstructionAccessFault => {
                exception_code = 1;
            }
            Exception::IllegalInstruction(_s) => {
                exception_code = 2;
            }
            Exception::Breakpoint => {
                exception_code = 3;
                cpu.mode = Mode::Debug;
                // "ECALL and EBREAK cause the receiving privilege mode’s epc register to be set to
                // the address of the ECALL or EBREAK instruction itself, not the address of the
                // following instruction."
                cpu.state.write(MEPC, exception_pc)?;
            }
            Exception::LoadAddressMisaligned => {
                exception_code = 4;
            }
            Exception::LoadAccessFault => {
                exception_code = 5;
            }
            Exception::StoreAMOAddressMisaligned => {
                exception_code = 6;
            }
            Exception::StoreAMOAccessFault => {
                exception_code = 7;
            }
            Exception::EnvironmentCallFromUMode => {
                exception_code = 8;

                // Move to the more privileged mode.
                cpu.mode = Mode::Machine;
                // "ECALL and EBREAK cause the receiving privilege mode’s epc register to be set to
                // the address of the ECALL or EBREAK instruction itself, not the address of the
                // following instruction."
                cpu.state.write(MEPC, exception_pc)?;

                match cpu.state.get(MTVEC)? {
                    Csr::Mtvec(mtvec) => match mtvec.read_mode() {
                        mtvec::Mode::Direct => {
                            cpu.pc = mtvec.read_base() as usize;
                        }
                        mtvec::Mode::Vectored => {
                            cpu.pc = (mtvec.read_base() + 4 * exception_code) as usize;
                        }
                        _ => {
                            return Err(Exception::IllegalInstruction(String::from(
                                "illegal mode in mtvec",
                            )))
                        }
                    },
                    _ => {
                        return Err(Exception::IllegalInstruction(String::from(
                            "failed to get a mtvec csr",
                        )))
                    }
                }
            }
            Exception::EnvironmentCallFromSMode => {
                exception_code = 9;

                // Move to the more privileged mode.
                cpu.mode = Mode::Machine;
                // "ECALL and EBREAK cause the receiving privilege mode’s epc register to be set to
                // the address of the ECALL or EBREAK instruction itself, not the address of the
                // following instruction."
                cpu.state.write(MEPC, exception_pc)?;

                match cpu.state.get(MTVEC)? {
                    Csr::Mtvec(mtvec) => match mtvec.read_mode() {
                        mtvec::Mode::Direct => {
                            cpu.pc = mtvec.read_base() as usize;
                        }
                        mtvec::Mode::Vectored => {
                            cpu.pc = (mtvec.read_base() + 4 * exception_code) as usize;
                        }
                        _ => {
                            return Err(Exception::IllegalInstruction(String::from(
                                "illegal mode in mtvec",
                            )))
                        }
                    },
                    _ => {
                        return Err(Exception::IllegalInstruction(String::from(
                            "failed to get a mtvec csr",
                        )))
                    }
                }
            }
            Exception::EnvironmentCallFromMMode => {
                exception_code = 11;
                match cpu.state.get(MTVEC)? {
                    Csr::Mtvec(mtvec) => match mtvec.read_mode() {
                        mtvec::Mode::Direct => {
                            cpu.pc = mtvec.read_base() as usize;
                        }
                        mtvec::Mode::Vectored => {
                            cpu.pc = (mtvec.read_base() + 4 * exception_code) as usize;
                        }
                        _ => {
                            return Err(Exception::IllegalInstruction(String::from(
                                "illegal mode in mtvec",
                            )))
                        }
                    },
                    _ => {
                        return Err(Exception::IllegalInstruction(String::from(
                            "failed to get a mtvec csr",
                        )))
                    }
                }
            }
            Exception::InstructionPageFault => {
                exception_code = 12;
            }
            Exception::LoadPageFault => {
                exception_code = 13;
            }
            Exception::StoreAMOPageFault => {
                exception_code = 15;
            }
        }

        match cpu.mode {
            Mode::Machine => {
                cpu.state.write(MCAUSE, 0 << 63 | exception_code)?;
                cpu.state.write(MEPC, exception_pc)?;
            }
            Mode::Supervisor => {
                cpu.state.write(SCAUSE, 0 << 63 | exception_code)?;
                cpu.state.write(SEPC, exception_pc)?;
            }
            Mode::User => {
                cpu.state.write(UCAUSE, 0 << 63 | exception_code)?;
                cpu.state.write(UEPC, exception_pc)?;
            }
            _ => {}
        }

        match self {
            Exception::InstructionAddressMisaligned(s) => {
                Err(Exception::InstructionAddressMisaligned(s.to_string()))
            }
            Exception::InstructionAccessFault => Err(Exception::InstructionAccessFault),
            Exception::IllegalInstruction(s) => Err(Exception::IllegalInstruction(s.to_string())),
            Exception::Breakpoint => Err(Exception::Breakpoint),
            Exception::LoadAddressMisaligned => Err(Exception::LoadAddressMisaligned),
            Exception::LoadAccessFault => Err(Exception::LoadAccessFault),
            Exception::StoreAMOAddressMisaligned => Err(Exception::StoreAMOAddressMisaligned),
            Exception::StoreAMOAccessFault => Err(Exception::StoreAMOAccessFault),
            Exception::EnvironmentCallFromUMode => Ok(()),
            Exception::EnvironmentCallFromSMode => Ok(()),
            Exception::EnvironmentCallFromMMode => Ok(()),
            Exception::InstructionPageFault => Err(Exception::InstructionPageFault),
            Exception::LoadPageFault => Err(Exception::LoadPageFault),
            Exception::StoreAMOPageFault => Err(Exception::StoreAMOPageFault),
        }
    }
}