From ab353c1beee8597954b58c2879b0f7c0f023c389 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 27 Feb 2023 20:21:16 +0800 Subject: [PATCH 1/4] loader: move the kernel stack to kernel region The kernel stack previously was located at the same relative offset as all other programs. While this worked, it is poor form. Move the kernel stack into the kernel memory region (located in the last 4 megabytes of virtual address). Signed-off-by: Sean Cross --- docs/memory.md | 5 +++-- loader/src/main.rs | 1 + loader/src/phase2.rs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/memory.md b/docs/memory.md index 0ff8842c2..b64c5e810 100644 --- a/docs/memory.md +++ b/docs/memory.md @@ -57,12 +57,13 @@ assigned to every process simply by mapping megapage 1023 (`0xffc00000`). | 0xff801000 | Context data (registers, etc.) | 0xff802000 | Return address from syscalls (never allocated) | 0xffc00000 | Kernel arguments, allocation tables +| 0xffcc0000 | Kernel GDB UART CSR page | 0xffcd0000 | Kernel WFI CSR page | 0xffce0000 | Kernel TRNG CSR page | 0xffcf0000 | Supervisor UART CSR page | 0xffd00000 | Kernel binary image and data section -| 0xfffefffc | "default" stack pointer (used by interrupt handlers) -| 0xffff0000 | Kernel stack top +| 0xfff80000 | Kernel stack top +| 0xffff0000 | "default" stack pointer (used by interrupt handlers) | 0xfff00000 | {unused} ``` diff --git a/loader/src/main.rs b/loader/src/main.rs index 7921639d5..6462cfc93 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -40,6 +40,7 @@ const USER_AREA_END: usize = 0xff00_0000; // All of the kernel structures must live within Megapage 1023, // and therefore are limited to 4 MB. const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +const KERNEL_STACK_TOP: usize = 0xfff8_0000; const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; const KERNEL_STACK_PAGE_COUNT: usize = 1; const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index f11f6426e..b12932e34 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -133,7 +133,7 @@ impl ProgramDescription { let pid_idx = (pid - 1) as usize; let is_kernel = pid == 1; let flag_defaults = FLG_R | FLG_W | FLG_VALID | if is_kernel { 0 } else { FLG_U }; - let stack_addr = USER_STACK_TOP - 16; + let stack_addr = if is_kernel { KERNEL_STACK_TOP } else { USER_STACK_TOP } - 16; if is_kernel { assert!(self.text_offset as usize == KERNEL_LOAD_OFFSET); assert!(((self.text_offset + self.text_size) as usize) < EXCEPTION_STACK_TOP); From 3cf4cafb054ffc651d2a979462b4f88b31a43281 Mon Sep 17 00:00:00 2001 From: eupn Date: Sat, 4 Mar 2023 18:15:48 +0400 Subject: [PATCH 2/4] xtask: support multiple different targets --- Cargo.lock | 1 + xtask/Cargo.toml | 1 + xtask/src/builder.rs | 40 ++++++++++++++++++++++++++------------ xtask/src/main.rs | 13 +++++++++++-- xtask/src/utils.rs | 46 ++++++++++++++++++++++++++++++++------------ 5 files changed, 75 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57c35446d..1c92a955e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5157,6 +5157,7 @@ dependencies = [ "atty", "chrono", "filetime", + "lazy_static", "rustc_version 0.4.0", "serde", "serde_json", diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index dc1b0b173..de83ff597 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -18,4 +18,5 @@ serde = { version = "1.0.130", features = ["derive"] } tempfile = "3.3.0" ureq = { version = "2.5.0", features = ["json"] } svd2utra = "0.1.11" +lazy_static = "1.4.0" # toml_edit = "0.14.4" # would be used to verify Cargo.toml files, but it's too hard to do this right now. \ No newline at end of file diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 83fe5a8aa..51a0443b5 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -5,7 +5,7 @@ use std::{ path::{Path, PathBuf}, process::Command, }; -use crate::{DynError, TARGET_TRIPLE, app_manifest::generate_app_menus, MemorySpec}; +use crate::{DynError, app_manifest::generate_app_menus, MemorySpec}; #[allow(dead_code)] #[derive(Copy, Clone, Debug)] @@ -148,7 +148,7 @@ impl Builder { features: Vec::new(), stream: BuildStream::Release, min_ver: crate::MIN_XOUS_VERSION.to_string(), - target: Some(crate::TARGET_TRIPLE.to_string()), + target: Some(crate::TARGET_TRIPLE_RISCV32.to_string()), utra_target: format!("precursor-{}", crate::PRECURSOR_SOC_VERSION).to_string(), run_svd2repl: false, locale_override: None, @@ -217,7 +217,7 @@ impl Builder { } /// Configure for renode targets pub fn target_renode<'a>(&'a mut self) -> &'a mut Builder { - self.target = Some(crate::TARGET_TRIPLE.to_string()); + self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string()); self.stream = BuildStream::Release; self.run_svd2repl = true; self.utra_target = "renode".to_string(); @@ -229,7 +229,7 @@ impl Builder { /// to call it anyways just in case the defaults change. The `soc_version` should /// be just the gitrev of the soc version, not the entire feature name. pub fn target_precursor<'a>(&'a mut self, soc_version: &str) -> &'a mut Builder { - self.target = Some(crate::TARGET_TRIPLE.to_string()); + self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string()); self.stream = BuildStream::Release; self.utra_target = format!("precursor-{}", soc_version).to_string(); self.run_svd2repl = false; @@ -238,6 +238,17 @@ impl Builder { self } + /// Configure for ARM targets + pub fn target_arm(&mut self) -> &mut Builder { + self.target = Some(crate::TARGET_TRIPLE_ARM.to_string()); + self.stream = BuildStream::Debug; + self.utra_target = "atsama5d27".to_string(); + self.run_svd2repl = false; + self.loader = CrateSpec::Local("loader".to_string(), false); + self.kernel = CrateSpec::Local("xous-kernel".to_string(), false); + self + } + /// Override the default kernel. For example, to use the kernel from crates.io, specify as "xous-kernel@0.9.9" #[allow(dead_code)] pub fn use_kernel<'a>(&'a mut self, spec: &str) -> &'a mut Builder { @@ -492,15 +503,13 @@ impl Builder { // no services were specified - don't build anything return Ok(()) } - crate::utils::ensure_compiler(&Some(TARGET_TRIPLE), false, false)?; - self.locale_override(); // apply the locale override // ------ configure target generation feature flags ------ let target = if self.utra_target.contains("renode") { self.features.push("renode".into()); self.loader_features.push("renode".into()); self.kernel_features.push("renode".into()); - Some(crate::TARGET_TRIPLE) + Some(crate::TARGET_TRIPLE_RISCV32) } else if self.utra_target.contains("hosted") { self.features.push("hosted".into()); // there is no loader in hosed mode @@ -513,11 +522,18 @@ impl Builder { self.kernel_features.push(format!("utralib/{}", &self.utra_target)); self.loader_features.push("precursor".into()); self.loader_features.push(format!("utralib/{}", &self.utra_target)); - Some(crate::TARGET_TRIPLE) + Some(crate::TARGET_TRIPLE_RISCV32) + } else if self.utra_target.contains("atsama5d2") { + self.kernel_features.push("atsama5d27".into()); + self.loader_features.push("atsama5d27".into()); + Some(crate::TARGET_TRIPLE_ARM) } else { return Err("Target unknown: please check your UTRA target".into()); }; + crate::utils::ensure_compiler(&self.target.as_ref().map(|s| s.as_str()), false, false)?; + self.locale_override(); // apply the locale override + // ------ build the services & apps ------ let mut app_names = Vec::::new(); for app in self.apps.iter() { @@ -653,7 +669,7 @@ impl Builder { } // ---------- extract SVD file path, as computed by utralib ---------- - let svd_spec_path = format!("target/{}/{}/build/SVD_PATH", TARGET_TRIPLE, self.stream.to_str()); + let svd_spec_path = format!("target/{}/{}/build/SVD_PATH", self.target.as_ref().expect("target"), self.stream.to_str()); let mut svd_spec_file = OpenOptions::new() .read(true) .open(&svd_spec_path)?; @@ -665,7 +681,7 @@ impl Builder { services_path.append(&mut self.enumerate_binary_files()?); // --------- package up and sign a binary image ---------- - let (inie, inif) = self.split_xip(services_path); + let (inie, inif) = self.split_xip(services_path.clone()); let output_bundle = self.create_image( &kernel_path[0], &inie, @@ -770,7 +786,7 @@ impl Builder { let mut output_file = PathBuf::new(); output_file.push("target"); - output_file.push(TARGET_TRIPLE); + output_file.push(self.target.as_ref().expect("target")); output_file.push(stream); output_file.push("xous_presign.img"); args.push(output_file.to_str().unwrap()); @@ -811,7 +827,7 @@ impl Builder { for item in [&self.services[..], &self.apps[..]].concat() { match item { CrateSpec::Prebuilt(name, url, _xip) => { - let exec_name = format!("target/{}/{}/{}", TARGET_TRIPLE, self.stream.to_str(), name); + let exec_name = format!("target/{}/{}/{}", self.target.as_ref().expect("target"), self.stream.to_str(), name); println!("Fetching {} executable from build server...", name); let mut exec_file = OpenOptions::new() .read(true) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f6eb79fe6..76473809a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -41,7 +41,9 @@ const PRECURSOR_SOC_VERSION: &str = "70190e2"; const MIN_XOUS_VERSION: &str = "v0.9.8-791"; /// target triple for precursor builds -const TARGET_TRIPLE: &str = "riscv32imac-unknown-xous-elf"; +pub(crate) const TARGET_TRIPLE_RISCV32: &str = "riscv32imac-unknown-xous-elf"; +/// target triple for ARM builds +pub(crate) const TARGET_TRIPLE_ARM: &str = "armv7a-unknown-xous-elf"; // because I have nowhere else to note this. The commit that contains the rkyv-enum derive // refactor to work around warnings thrown by Rust 1.64.0 is: f815ed85b58b671178fbf53b4cea34186fc406eb @@ -159,7 +161,7 @@ fn main() -> Result<(), Box> { Some("install-toolkit") | Some("install-toolchain") => { let arg = env::args().nth(2); ensure_compiler( - &Some(TARGET_TRIPLE), + &Some(TARGET_TRIPLE_RISCV32), true, arg.map(|x| x == "--force").unwrap_or(false), )? @@ -369,6 +371,13 @@ fn main() -> Result<(), Box> { .add_feature("avalanchetest"); } + // ------ ARM hardware image configs ------ + Some("arm-tiny") => { + builder.target_arm() + .add_services(&base_pkgs.into_iter().map(String::from).collect()) + .add_services(&get_cratespecs()); + } + // ---- other single-purpose commands ---- Some("generate-locales") => generate_locales()?, Some("wycheproof-import") => whycheproof_import()?, diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs index 936acbcd9..fb5042e54 100644 --- a/xtask/src/utils.rs +++ b/xtask/src/utils.rs @@ -1,3 +1,5 @@ +use lazy_static::lazy_static; +use std::collections::HashMap; use std::{ fs::{File, OpenOptions}, io::{Read, Write}, @@ -5,10 +7,22 @@ use std::{ process::Command, }; -use crate::TARGET_TRIPLE; use crate::{cargo, project_root}; +use crate::{TARGET_TRIPLE_RISCV32, TARGET_TRIPLE_ARM}; const TOOLCHAIN_RELEASE_URL: &str = "https://api.github.com/repos/betrusted-io/rust/releases"; +const TOOLCHAIN_RELEASE_URL_ARM: &str = + "https://api.github.com/repos/Foundation-Devices/rust/releases"; + +lazy_static! { + static ref TOOLCHAIN_RELEASE_URLS: HashMap = HashMap::from([ + (TARGET_TRIPLE_RISCV32.to_owned(), TOOLCHAIN_RELEASE_URL.to_owned()), + ( + TARGET_TRIPLE_ARM.to_owned(), + TOOLCHAIN_RELEASE_URL_ARM.to_owned() + ), + ]); +} /// Since we use the same TARGET for all calls to `build()`, /// cache it inside an atomic boolean. If this is `true` then @@ -86,12 +100,12 @@ pub(crate) fn ensure_compiler( } // If the sysroot exists, then we're good. - let target = target.unwrap_or(TARGET_TRIPLE); + let target = target.unwrap_or(TARGET_TRIPLE_RISCV32); if let Some(path) = get_sysroot(Some(target))? { let mut version_path = PathBuf::from(&path); version_path.push("lib"); version_path.push("rustlib"); - version_path.push(TARGET_TRIPLE); + version_path.push(target); if remove_existing { println!("Target path exists, removing it"); std::fs::remove_dir_all(version_path) @@ -99,13 +113,13 @@ pub(crate) fn ensure_compiler( println!("Also removing target directories for existing toolchain"); let mut target_main = project_root(); target_main.push("target"); - target_main.push(TARGET_TRIPLE); + target_main.push(target); std::fs::remove_dir_all(target_main).ok(); let mut target_loader = project_root(); target_loader.push("loader"); target_loader.push("target"); - target_loader.push(TARGET_TRIPLE); + target_loader.push(target); std::fs::remove_dir_all(target_loader).ok(); } else { DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst); @@ -162,8 +176,16 @@ pub(crate) fn ensure_compiler( println!(); } - fn get_toolchain_url(major: u64, minor: u64, patch: u64) -> Result { - let j: serde_json::Value = ureq::get(TOOLCHAIN_RELEASE_URL) + fn get_toolchain_url( + target: &str, + major: u64, + minor: u64, + patch: u64, + ) -> Result { + let url = TOOLCHAIN_RELEASE_URLS + .get(target) + .ok_or_else(|| format!("Can't find toolchain URL for target {}", target))?; + let j: serde_json::Value = ureq::get(&url) .set("Accept", "application/vnd.github.v3+json") .call() .map_err(|e| format!("{}", e))? @@ -218,12 +240,12 @@ pub(crate) fn ensure_compiler( } if let Some((_k, v)) = tag_urls.into_iter().last() { - // println!("Found candidate entry: v{} url {}", k, v); + // println!("Found candidate entry: v{} url {}", _k, v); return Ok(v); } Err(format!("No toolchains found for Rust {}", target_prefix)) } - let toolchain_url = get_toolchain_url(ver.major, ver.minor, ver.patch)?; + let toolchain_url = get_toolchain_url(target, ver.major, ver.minor, ver.patch)?; println!( "Attempting to install toolchain for {} into {}", @@ -377,8 +399,8 @@ pub(crate) fn whycheproof_import() -> Result<(), crate::DynError> { } pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynError> { - let last_config = format!("target/{}/LAST_LANG", TARGET_TRIPLE); - std::fs::create_dir_all(format!("target/{}/", TARGET_TRIPLE)).unwrap(); + let last_config = format!("target/{}/LAST_LANG", TARGET_TRIPLE_RISCV32); + std::fs::create_dir_all(format!("target/{}/", TARGET_TRIPLE_RISCV32)).unwrap(); let mut contents = String::new(); let changed = match OpenOptions::new() @@ -407,4 +429,4 @@ pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynEr println!("No change to the target locale language of {}", contents); } Ok(()) -} \ No newline at end of file +} From f347b4653091394ae72988c3738b9e34737e67a5 Mon Sep 17 00:00:00 2001 From: eupn Date: Sun, 5 Mar 2023 13:59:16 +0400 Subject: [PATCH 3/4] xtask: unify stdlib url constant naming --- xtask/src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs index fb5042e54..8b19d841b 100644 --- a/xtask/src/utils.rs +++ b/xtask/src/utils.rs @@ -10,13 +10,13 @@ use std::{ use crate::{cargo, project_root}; use crate::{TARGET_TRIPLE_RISCV32, TARGET_TRIPLE_ARM}; -const TOOLCHAIN_RELEASE_URL: &str = "https://api.github.com/repos/betrusted-io/rust/releases"; +const TOOLCHAIN_RELEASE_URL_RISCV32: &str = "https://api.github.com/repos/betrusted-io/rust/releases"; const TOOLCHAIN_RELEASE_URL_ARM: &str = "https://api.github.com/repos/Foundation-Devices/rust/releases"; lazy_static! { static ref TOOLCHAIN_RELEASE_URLS: HashMap = HashMap::from([ - (TARGET_TRIPLE_RISCV32.to_owned(), TOOLCHAIN_RELEASE_URL.to_owned()), + (TARGET_TRIPLE_RISCV32.to_owned(), TOOLCHAIN_RELEASE_URL_RISCV32.to_owned()), ( TARGET_TRIPLE_ARM.to_owned(), TOOLCHAIN_RELEASE_URL_ARM.to_owned() From 058a42d9da4fd4d5c7228f5412fb4969cc173097 Mon Sep 17 00:00:00 2001 From: eupn Date: Sun, 5 Mar 2023 13:59:50 +0400 Subject: [PATCH 4/4] xtask: put LAST_LANG at the target root and omit dir creation --- xtask/src/utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs index 8b19d841b..a2e12d740 100644 --- a/xtask/src/utils.rs +++ b/xtask/src/utils.rs @@ -399,8 +399,7 @@ pub(crate) fn whycheproof_import() -> Result<(), crate::DynError> { } pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynError> { - let last_config = format!("target/{}/LAST_LANG", TARGET_TRIPLE_RISCV32); - std::fs::create_dir_all(format!("target/{}/", TARGET_TRIPLE_RISCV32)).unwrap(); + let last_config = "target/LAST_LANG"; let mut contents = String::new(); let changed = match OpenOptions::new()