|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use quote::{quote, quote_spanned}; |
| 3 | +use std::{fmt::Display, ops::RangeInclusive}; |
| 4 | +use syn::{ |
| 5 | + parse::Parser, punctuated::Punctuated, spanned::Spanned, token::Comma, Expr, ExprLit, ExprPath, |
| 6 | + ExprRange, ExprReference, Lit, RangeLimits, |
| 7 | +}; |
| 8 | + |
| 9 | +extern crate proc_macro; |
| 10 | + |
| 11 | +const RESERVED_HANDLERS: &[u8] = &[15, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31]; |
| 12 | +const HANDLERS_WITH_ERR_CODE: &[u8] = &[8, 10, 11, 12, 13, 14, 17, 30]; |
| 13 | + |
| 14 | +#[proc_macro] |
| 15 | +pub fn set_general_handler(item: TokenStream) -> TokenStream { |
| 16 | + set_general_handler_impl(item).unwrap_or_else(|err| err.to_compile_error().into()) |
| 17 | +} |
| 18 | + |
| 19 | +fn set_general_handler_impl(item: TokenStream) -> syn::Result<TokenStream> { |
| 20 | + let input = Punctuated::<Expr, Comma>::parse_separated_nonempty.parse(item)?; |
| 21 | + let (idt, function, range) = parse_input(&input)?; |
| 22 | + |
| 23 | + let mut handlers = Vec::new(); |
| 24 | + for index in range { |
| 25 | + if RESERVED_HANDLERS.contains(&index) { |
| 26 | + continue; // skip reserved handlers |
| 27 | + } |
| 28 | + let function_call = if HANDLERS_WITH_ERR_CODE.contains(&index) { |
| 29 | + quote_spanned!(function.span() => { |
| 30 | + #function(stack_frame, #index.into(), Some(error_code)); |
| 31 | + }) |
| 32 | + } else { |
| 33 | + quote_spanned!(function.span() => { |
| 34 | + #function(stack_frame, #index.into(), None); |
| 35 | + }) |
| 36 | + }; |
| 37 | + |
| 38 | + let stack_frame_arg = |
| 39 | + quote! { stack_frame: &mut x86_64::structures::idt::InterruptStackFrame }; |
| 40 | + |
| 41 | + let handler_with_err_code = quote! { |
| 42 | + extern "x86-interrupt" fn handler(#stack_frame_arg, error_code: u64) { |
| 43 | + #function_call |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + let set_handler_fn = match index { |
| 48 | + 8 => quote! { |
| 49 | + extern "x86-interrupt" fn handler(#stack_frame_arg, error_code: u64) -> ! { |
| 50 | + #function_call |
| 51 | + panic!("General handler returned on double fault"); |
| 52 | + } |
| 53 | + (#idt).double_fault.set_handler_fn(handler); |
| 54 | + }, |
| 55 | + 10 => quote! { |
| 56 | + #handler_with_err_code |
| 57 | + (#idt).invalid_tss.set_handler_fn(handler); |
| 58 | + }, |
| 59 | + 11 => quote! { |
| 60 | + #handler_with_err_code |
| 61 | + (#idt).segment_not_present.set_handler_fn(handler); |
| 62 | + }, |
| 63 | + 12 => quote! { |
| 64 | + #handler_with_err_code |
| 65 | + (#idt).stack_segment_fault.set_handler_fn(handler); |
| 66 | + }, |
| 67 | + 13 => quote! { |
| 68 | + #handler_with_err_code |
| 69 | + (#idt).general_protection_fault.set_handler_fn(handler); |
| 70 | + }, |
| 71 | + 14 => quote! { |
| 72 | + extern "x86-interrupt" fn handler(#stack_frame_arg, error_code: x86_64::structures::idt::PageFaultErrorCode) { |
| 73 | + let error_code = error_code.bits(); |
| 74 | + #function_call |
| 75 | + } |
| 76 | + (#idt).page_fault.set_handler_fn(handler); |
| 77 | + }, |
| 78 | + 17 => quote! { |
| 79 | + #handler_with_err_code |
| 80 | + (#idt).alignment_check.set_handler_fn(handler); |
| 81 | + }, |
| 82 | + 18 => quote! { |
| 83 | + extern "x86-interrupt" fn handler(#stack_frame_arg) -> ! { |
| 84 | + #function_call |
| 85 | + panic!("General handler returned on machine check exception"); |
| 86 | + } |
| 87 | + (#idt).machine_check.set_handler_fn(handler); |
| 88 | + }, |
| 89 | + 30 => quote! { |
| 90 | + #handler_with_err_code |
| 91 | + (#idt).security_exception.set_handler_fn(handler); |
| 92 | + }, |
| 93 | + index => quote! { |
| 94 | + extern "x86-interrupt" fn handler(#stack_frame_arg) { |
| 95 | + #function_call |
| 96 | + } |
| 97 | + (#idt)[#index.into()].set_handler_fn(handler); |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + // double `{{` to create new scope |
| 102 | + handlers.push(quote! {{ |
| 103 | + #set_handler_fn |
| 104 | + }}); |
| 105 | + } |
| 106 | + |
| 107 | + let ret = quote! { |
| 108 | + #(#handlers)* |
| 109 | + }; |
| 110 | + |
| 111 | + // uncomment to print generated code: |
| 112 | + // println!("{}", ret); |
| 113 | + |
| 114 | + Ok(ret.into()) |
| 115 | +} |
| 116 | + |
| 117 | +fn parse_input( |
| 118 | + input: &Punctuated<Expr, Comma>, |
| 119 | +) -> syn::Result<(&ExprReference, &ExprPath, RangeInclusive<u8>)> { |
| 120 | + if input.len() < 2 || input.len() > 3 { |
| 121 | + return Err(err(input, "expected 2 or 3 arguments")); |
| 122 | + } |
| 123 | + |
| 124 | + let idt = match &input[0] { |
| 125 | + Expr::Reference(r) if r.mutability.is_some() => r, |
| 126 | + other => return Err(err(other, "expected a `&mut` reference to an IDT")), |
| 127 | + }; |
| 128 | + |
| 129 | + let function = match &input[1] { |
| 130 | + Expr::Path(path) => path, |
| 131 | + other => return Err(err(other, "expected the name of a handler function")), |
| 132 | + }; |
| 133 | + |
| 134 | + let range = if input.len() == 3 { |
| 135 | + match &input[2] { |
| 136 | + Expr::Lit(lit) => match &lit.lit { |
| 137 | + Lit::Int(int) => { |
| 138 | + let index: u8 = int.base10_parse()?; |
| 139 | + index..=index |
| 140 | + } |
| 141 | + other => return Err(err(other, "expected index or range")), |
| 142 | + }, |
| 143 | + Expr::Range(range) => parse_range(&range)?, |
| 144 | + other => return Err(err(other, "expected index or range")), |
| 145 | + } |
| 146 | + } else { |
| 147 | + 0..=255 |
| 148 | + }; |
| 149 | + |
| 150 | + Ok((idt, function, range)) |
| 151 | +} |
| 152 | + |
| 153 | +fn parse_range(range: &ExprRange) -> syn::Result<RangeInclusive<u8>> { |
| 154 | + let range_start: u8 = match range.from.as_deref() { |
| 155 | + Some(&Expr::Lit(ExprLit { |
| 156 | + lit: Lit::Int(ref int), |
| 157 | + .. |
| 158 | + })) => int.base10_parse()?, |
| 159 | + Some(other) => return Err(err(other, "Invalid range start")), |
| 160 | + None => 0, |
| 161 | + }; |
| 162 | + let mut range_end: u8 = match range.to.as_deref() { |
| 163 | + Some(&Expr::Lit(ExprLit { |
| 164 | + lit: Lit::Int(ref int), |
| 165 | + .. |
| 166 | + })) => int.base10_parse()?, |
| 167 | + Some(other) => return Err(err(other, "Invalid range end")), |
| 168 | + None => 255, |
| 169 | + }; |
| 170 | + if let ExprRange { |
| 171 | + limits: RangeLimits::HalfOpen(_), |
| 172 | + to: Some(_), |
| 173 | + .. |
| 174 | + } = range |
| 175 | + { |
| 176 | + range_end = range_end |
| 177 | + .checked_sub(1) |
| 178 | + .ok_or_else(|| err(&range.to, "Invalid range"))?; |
| 179 | + }; |
| 180 | + |
| 181 | + Ok(range_start..=range_end) |
| 182 | +} |
| 183 | + |
| 184 | +fn err(tokens: impl quote::ToTokens, message: impl Display) -> syn::Error { |
| 185 | + syn::Error::new_spanned(tokens, message) |
| 186 | +} |
0 commit comments