|
| 1 | +use std::{collections::HashMap, fs::File, path::Path}; |
| 2 | + |
| 3 | +extern crate rand; |
| 4 | +extern crate syn; |
| 5 | +extern crate walkdir; |
| 6 | + |
| 7 | +use cbindgen::LayoutConfig; |
| 8 | +use std::io::Write; |
| 9 | +use syn::{Attribute, LitInt}; |
| 10 | +use walkdir::WalkDir; |
| 11 | + |
| 12 | +extern crate cbindgen; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + println!("cargo:rerun-if-changed=src"); |
| 16 | + println!("cargo:rerun-if-changed=build.rs"); |
| 17 | + |
| 18 | + let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 19 | + |
| 20 | + let mut config: cbindgen::Config = Default::default(); |
| 21 | + |
| 22 | + config.no_includes = true; |
| 23 | + config.includes = vec![ |
| 24 | + "stdint.h".to_string(), |
| 25 | + "stdbool.h".to_string(), |
| 26 | + "stdarg.h".to_string(), |
| 27 | + ]; |
| 28 | + config.layout = LayoutConfig { |
| 29 | + packed: Some("__attribute__((packed))".to_string()), |
| 30 | + ..Default::default() |
| 31 | + }; |
| 32 | + |
| 33 | + cbindgen::Builder::new() |
| 34 | + .with_crate(crate_dir) |
| 35 | + .with_config(config) |
| 36 | + .with_language(cbindgen::Language::C) |
| 37 | + .with_include_guard("KERNEL_H") |
| 38 | + .generate() |
| 39 | + .expect("Unable to generate bindings") |
| 40 | + .write_to_file("include/kernel/lib.h"); |
| 41 | + |
| 42 | + generate_syscall_map("src").expect("Failed to generate syscall map."); |
| 43 | +} |
| 44 | + |
| 45 | +fn generate_syscall_map<P: AsRef<Path>>(root: P) -> Result<(), std::io::Error> { |
| 46 | + let syscalls = collect_syscalls(root); |
| 47 | + |
| 48 | + let mut file = File::create("../include/syscalls.map.gen.h")?; |
| 49 | + |
| 50 | + writeln!(file, "#ifndef SYSCALLS_MAP_GEN_H")?; |
| 51 | + writeln!(file, "#define SYSCALLS_MAP_GEN_H")?; |
| 52 | + |
| 53 | + writeln!(file)?; |
| 54 | + |
| 55 | + writeln!(file, "#include <stdint.h>")?; |
| 56 | + |
| 57 | + writeln!(file)?; |
| 58 | + |
| 59 | + writeln!( |
| 60 | + file, |
| 61 | + "#define DECLARE_SYSCALL(name, num) case num: name(svc_args); break;" |
| 62 | + )?; |
| 63 | + |
| 64 | + writeln!(file)?; |
| 65 | + |
| 66 | + writeln!(file, "#define DECLARE_SYSCALLS() \\")?; |
| 67 | + for (name, _) in syscalls.clone() { |
| 68 | + writeln!(file, "extern void {}(void *svc_args); \\", name)?; |
| 69 | + } |
| 70 | + |
| 71 | + writeln!(file)?; |
| 72 | + |
| 73 | + writeln!(file, "#define IMPLEMENT_SYSCALLS() \\")?; |
| 74 | + for (name, (number, _argc)) in syscalls { |
| 75 | + writeln!(file, " DECLARE_SYSCALL({}, {})", name, number)?; |
| 76 | + } |
| 77 | + |
| 78 | + writeln!(file)?; |
| 79 | + |
| 80 | + writeln!(file, "#endif //SYSCALLS_MAP_GEN_H")?; |
| 81 | + |
| 82 | + Ok(()) |
| 83 | +} |
| 84 | + |
| 85 | +fn is_syscall(attrs: &[Attribute]) -> Option<(u8, u8)> { |
| 86 | + let mut args = 0; |
| 87 | + let mut num = 0; |
| 88 | + |
| 89 | + for attr in attrs { |
| 90 | + if attr.path().is_ident("syscall_handler") { |
| 91 | + attr.parse_nested_meta(|meta| { |
| 92 | + if meta.path.is_ident("args") { |
| 93 | + let raw = meta.value()?; |
| 94 | + let value: LitInt = raw.parse()?; |
| 95 | + args = value.base10_parse()?; |
| 96 | + |
| 97 | + if !(0..=4).contains(&args) { |
| 98 | + return Err(meta.error("invalid number of arguments")); |
| 99 | + } |
| 100 | + |
| 101 | + return Ok(()); |
| 102 | + } |
| 103 | + |
| 104 | + if meta.path.is_ident("num") { |
| 105 | + let raw = meta.value()?; |
| 106 | + let value: LitInt = raw.parse()?; |
| 107 | + num = value.base10_parse()?; |
| 108 | + |
| 109 | + if !(0..=255).contains(&num) { |
| 110 | + return Err(meta.error("invalid syscall number")); |
| 111 | + } |
| 112 | + |
| 113 | + return Ok(()); |
| 114 | + } |
| 115 | + |
| 116 | + Err(meta.error("unknown attribute")) |
| 117 | + }) |
| 118 | + .expect("failed to parse attribute"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if args == 0 || num == 0 { |
| 123 | + return None; |
| 124 | + } |
| 125 | + |
| 126 | + Some((num, args)) |
| 127 | +} |
| 128 | + |
| 129 | +type SyscallData = (u8, u8); |
| 130 | + |
| 131 | +fn collect_syscalls<P: AsRef<Path>>(root: P) -> HashMap<String, SyscallData> { |
| 132 | + let mut syscalls = HashMap::new(); |
| 133 | + let mut numbers = HashMap::new(); |
| 134 | + |
| 135 | + for entry in WalkDir::new(root) { |
| 136 | + let entry = match entry { |
| 137 | + Ok(entry) => entry, |
| 138 | + Err(_) => continue, |
| 139 | + }; |
| 140 | + |
| 141 | + if entry.file_type().is_file() { |
| 142 | + let path = entry.path(); |
| 143 | + |
| 144 | + let contents = match std::fs::read_to_string(path) { |
| 145 | + Ok(contents) => contents, |
| 146 | + Err(_) => continue, |
| 147 | + }; |
| 148 | + |
| 149 | + let file = match syn::parse_file(&contents) { |
| 150 | + Ok(file) => file, |
| 151 | + Err(_) => continue, |
| 152 | + }; |
| 153 | + |
| 154 | + for item in file.items { |
| 155 | + let item = match item { |
| 156 | + syn::Item::Fn(item) => item, |
| 157 | + _ => continue, |
| 158 | + }; |
| 159 | + |
| 160 | + if !item |
| 161 | + .sig |
| 162 | + .abi |
| 163 | + .is_some_and(|abi| abi.name.is_some_and(|name| name.value() == "C")) |
| 164 | + { |
| 165 | + continue; |
| 166 | + } |
| 167 | + |
| 168 | + if let Some((num, argc)) = is_syscall(&item.attrs) { |
| 169 | + let name = item.sig.ident.to_string(); |
| 170 | + |
| 171 | + if syscalls.contains_key(&name) { |
| 172 | + eprintln!("Duplicate syscall handler: {}", name); |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + syscalls.insert(name.clone(), (num, argc)); |
| 177 | + numbers.insert(num, name); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + syscalls |
| 184 | +} |
0 commit comments