├── .github ├── CODEOWNERS └── bors.toml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── src ├── lib.rs └── test.rs └── triagebot.toml /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * rust-embedded/cortex-a rust-embedded/cortex-m rust-embedded/cortex-r rust-embedded/msp430 rust-embedded/riscv 2 | -------------------------------------------------------------------------------- /.github/bors.toml: -------------------------------------------------------------------------------- 1 | block_labels = ["needs-decision"] 2 | delete_merged_branches = true 3 | required_approvals = 1 4 | status = ["continuous-integration/travis-ci/push"] 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.rs.bk 2 | Cargo.lock 3 | target 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | matrix: 4 | allow_failures: 5 | - rust: nightly 6 | include: 7 | - rust: stable 8 | - rust: nightly 9 | # MSRV 10 | - rust: 1.31.0 11 | 12 | branches: 13 | only: 14 | - master 15 | - staging 16 | - trying 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/). 5 | 6 | ## [Unreleased] 7 | 8 | ## [v1.0.0] - 2020-03-03 9 | 10 | ### Breaking Changes 11 | 12 | - Remove the init array APIs as they are not pulling their weight. 13 | - Bound `zero_bss` and `init_data` by a new `Word` trait. 14 | 15 | ## [v0.2.2] - 2017-07-21 16 | 17 | ### Changed 18 | 19 | - Optimized `zero_bss` and `init_data` for binary size. This change also results 20 | in faster routines on non Cortex-M architectures. 21 | 22 | ## [v0.2.1] - 2017-04-08 23 | 24 | ### Added 25 | 26 | - `.init_array` / `.pre_init_array` support 27 | 28 | ## [v0.2.0] - 2017-01-22 29 | 30 | ### Changed 31 | 32 | - [breaking-change] The signature of the `zero_bss` and `init_data` functions; 33 | the end `.bss`/`.data` pointer now must be `*mut T`. This makes it impossible 34 | for people to bind a `static`, i.e. not `static mut`, variable to the end 35 | of these sections, which could make the compiler, or other programmer, think 36 | that such variable is allocated in the `.rodata` or the `.text` section. 37 | 38 | ## v0.1.0 - 2016-10-03 39 | 40 | ### Added 41 | 42 | - `init_data` and `zero_bss` functions. 43 | 44 | [Unreleased]: https://github.com/rust-embedded/r0/compare/v1.0.0...HEAD 45 | [v1.0.0]: https://github.com/rust-embedded/r0/compare/v0.2.2...v1.0.0 46 | [v0.2.2]: https://github.com/rust-embedded/r0/compare/v0.2.1...v0.2.2 47 | [v0.2.1]: https://github.com/rust-embedded/r0/compare/v0.2.0...v0.2.1 48 | [v0.2.0]: https://github.com/rust-embedded/r0/compare/v0.1.0...v0.2.0 49 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # The Rust Code of Conduct 2 | 3 | ## Conduct 4 | 5 | **Contact**: [Embedded WG][wg] 6 | 7 | * We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, nationality, or other similar characteristic. 8 | * On Matrix and IRC, please avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and welcoming environment for all. 9 | * Please be kind and courteous. There's no need to be mean or rude. 10 | * Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and numerous costs. There is seldom a right answer. 11 | * Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and see how it works. 12 | * We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behavior. We interpret the term "harassment" as including the definition in the [Citizen Code of Conduct](http://citizencodeofconduct.org/); if you have any lack of clarity about what might be included in that concept, please read their definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups. 13 | * Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or made uncomfortable by a community member, please contact one of the channel ops or any of the [Embedded WG][wg] immediately. Whether you're a regular contributor or a newcomer, we care about making this community a safe place for you and we've got your back. 14 | * Likewise any spamming, trolling, flaming, baiting or other attention-stealing behavior is not welcome. 15 | 16 | ## Moderation 17 | 18 | These are the policies for upholding our community's standards of conduct. 19 | 20 | 1. Remarks that violate the Rust standards of conduct, including hateful, hurtful, oppressive, or exclusionary remarks, are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.) 21 | 2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed. 22 | 3. Moderators will first respond to such remarks with a warning. 23 | 4. If the warning is unheeded, the user will be "kicked," i.e., kicked out of the communication channel to cool off. 24 | 5. If the user comes back and continues to make trouble, they will be banned, i.e., indefinitely excluded. 25 | 6. Moderators may choose at their discretion to un-ban the user if it was a first offense and they offer the offended party a genuine apology. 26 | 7. If a moderator bans someone and you think it was unjustified, please take it up with that moderator, or with a different moderator, **in private**. Complaints about bans in-channel are not allowed. 27 | 8. Moderators are held to a higher standard than other community members. If a moderator creates an inappropriate situation, they should expect less leeway than others. 28 | 29 | In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can drive people away from the community entirely. 30 | 31 | And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good there was something you could've communicated better — remember that it's your responsibility to make your fellow Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their trust. 32 | 33 | The enforcement policies listed above apply to all official embedded WG venues; including the official Matrix room (#rust-embedded:matrix.org) and linked IRC channels (#rust-embedded on Freenode); GitHub repositories under rust-embedded; and all forums under rust-embedded.org (forum.rust-embedded.org). 34 | 35 | *Adapted from the [Node.js Policy on Trolling](http://blog.izs.me/post/30036893703/policy-on-trolling) as well as the [Contributor Covenant v1.3.0](https://www.contributor-covenant.org/version/1/3/0/).* 36 | 37 | [wg]: https://github.com/rust-embedded/wg#organization 38 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "r0" 3 | authors = [ 4 | "Rust Embedded Working Group ", 5 | ] 6 | categories = ["embedded", "no-std"] 7 | description = "Initialization code ('crt0') written in Rust" 8 | documentation = "https://docs.rs/r0" 9 | edition = "2018" 10 | keywords = ["initialization", "init", "start", "crt0", "c0"] 11 | license = "MIT OR Apache-2.0" 12 | readme = "README.md" 13 | repository = "https://github.com/rust-embedded/r0" 14 | version = "1.0.0" # don't forget to update html_root_url 15 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Jorge Aparicio 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # r0 2 | 3 | [![Build status](https://api.travis-ci.org/rust-embedded/r0.svg?branch=master)](https://travis-ci.org/rust-embedded/r0) 4 | [![crates.io](https://img.shields.io/crates/d/r0.svg)](https://crates.io/crates/r0) 5 | [![crates.io](https://img.shields.io/crates/v/r0.svg)](https://crates.io/crates/r0) 6 | 7 | **DEPRECATED: This crate is no longer supported, and dependents are instead 8 | recommended to implement their r0 using assembly. See [cortex-m-rt#300] 9 | for details.** 10 | 11 | [cortex-m-rt#300]: https://github.com/rust-embedded/cortex-m-rt/issues/300 12 | 13 | Memory initialization code written in Rust. 14 | 15 | This crate is for bare metal systems where there is no ELF loader or OS to take care of 16 | initializing RAM for the program. 17 | 18 | r0 is not meant to be used by user applications directly. Instead, it is most often used by 19 | embedded runtime crates, like: 20 | 21 | * [cortex-m-rt](https://github.com/rust-embedded/cortex-m-rt) 22 | * [riscv-rt](https://github.com/rust-embedded/riscv-rt) 23 | * [msp430-rt](https://github.com/rust-embedded/msp430-rt) 24 | 25 | The r0 crate provides similar functionality to [crt0](https://en.wikipedia.org/wiki/Crt0) in the C 26 | runtime. 27 | 28 | This project is developed and maintained by the [Cortex-A, Cortex-M, Cortex-R, MSP430, and RISCV 29 | teams][teams]. 30 | 31 | ## [Documentation](https://docs.rs/r0) 32 | 33 | ## Minimum Supported Rust Version (MSRV) 34 | 35 | This crate is guaranteed to compile on stable Rust 1.31.0 and up. It *might* 36 | compile with older versions but that may change in any new patch release. 37 | 38 | ## License 39 | 40 | Licensed under either of 41 | 42 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or 43 | ) 44 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or ) 45 | 46 | at your option. 47 | 48 | ### Contribution 49 | 50 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the 51 | work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 52 | additional terms or conditions. 53 | 54 | ## Code of Conduct 55 | 56 | Contribution to this crate is organized under the terms of the [Rust Code of 57 | Conduct][CoC], the maintainers of this crate, the [Cortex-A, Cortex-M, 58 | Cortex-R, MSP430, and RISCV teams][teams], promise to intervene to uphold that 59 | code of conduct. 60 | 61 | [CoC]: CODE_OF_CONDUCT.md 62 | [teams]: https://github.com/rust-embedded/wg#organization 63 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Memory initialization code ("[crt0]") written in Rust. 2 | //! 3 | //! This crate is meant for bare metal systems where there is no ELF loader or 4 | //! OS to take care of initializing RAM for the program. It provides functions 5 | //! for initializing the `.data` and `.bss` sections. 6 | //! 7 | //! [crt0]: https://en.wikipedia.org/wiki/Crt0 8 | //! 9 | //! # Initializing RAM 10 | //! 11 | //! On the linker script side, we must assign names (symbols) to the boundaries 12 | //! of the `.bss` and `.data` sections. For example: 13 | //! 14 | //! ```text 15 | //! .bss : ALIGN(4) 16 | //! { 17 | //! _sbss = .; 18 | //! *(.bss.*); 19 | //! _ebss = ALIGN(4); 20 | //! } > RAM 21 | //! 22 | //! .data : ALIGN(4) 23 | //! { 24 | //! _sdata = .; 25 | //! *(.data.*); 26 | //! _edata = ALIGN(4); 27 | //! } > RAM AT > FLASH 28 | //! 29 | //! _sidata = LOADADDR(.data); 30 | //! ``` 31 | //! 32 | //! This script defines symbols `_sbss`/`_ebss`, and `_sdata`/`_edata` to point 33 | //! at the boundaries of the `.bss` and `.data` sections in RAM, respectively. 34 | //! The `AT > FLASH` directive places the actual contents of the `.data` section 35 | //! in the `FLASH` memory region (which needs to be defined separately from this 36 | //! linker script snippet). Then `_sidata` is set to the address of that data in 37 | //! flash. 38 | //! 39 | //! Note that while `_sbss`, `_ebss`, `_sdata` and `_edata` are Virtual Memory 40 | //! Addresses (VMAs), `_sidata` is the Load Memory Address (LMA) of the `.data` 41 | //! section. 42 | //! 43 | //! On the Rust side, we must bind to those symbols using an `extern` block, 44 | //! and can then call into this crate to perform RAM initialization: 45 | //! 46 | //! ```no_run 47 | //! # use r0::{zero_bss, init_data}; 48 | //! unsafe fn before_main() { 49 | //! // The type, `u32`, indicates that the memory is 4-byte aligned 50 | //! extern "C" { 51 | //! static mut _sbss: u32; 52 | //! static mut _ebss: u32; 53 | //! 54 | //! static mut _sdata: u32; 55 | //! static mut _edata: u32; 56 | //! 57 | //! static _sidata: u32; 58 | //! } 59 | //! 60 | //! zero_bss(&mut _sbss, &mut _ebss); 61 | //! init_data(&mut _sdata, &mut _edata, &_sidata); 62 | //! } 63 | //! ``` 64 | //! 65 | //! # Minimum Supported Rust Version (MSRV) 66 | //! 67 | //! The MSRV of this release is Rust 1.31.0 68 | 69 | #![deny(warnings)] 70 | #![no_std] 71 | #![doc(html_root_url = "https://docs.rs/r0/1.0.0")] 72 | 73 | #[cfg(test)] 74 | mod test; 75 | 76 | use core::sync::atomic::{compiler_fence, Ordering}; 77 | use core::{mem, ptr}; 78 | 79 | mod sealed { 80 | pub trait Sealed {} 81 | } 82 | 83 | /// Trait for machine word types. 84 | /// 85 | /// This trait is implemented by unsigned integers representing common machine 86 | /// word sizes. It can not be implemented by the user. 87 | /// 88 | /// Types implementing this trait can be used by the [`init_data`] and 89 | /// [`zero_bss`] functions. For that to be sound, all bit patterns need to be 90 | /// valid for the type, the type must implement `Copy`, and the type must not 91 | /// be zero-sized. 92 | /// 93 | /// [`init_data`]: fn.init_data.html 94 | /// [`zero_bss`]: fn.zero_bss.html 95 | pub unsafe trait Word: sealed::Sealed + Copy {} 96 | 97 | impl sealed::Sealed for u8 {} 98 | impl sealed::Sealed for u16 {} 99 | impl sealed::Sealed for u32 {} 100 | impl sealed::Sealed for u64 {} 101 | impl sealed::Sealed for u128 {} 102 | 103 | unsafe impl Word for u8 {} 104 | unsafe impl Word for u16 {} 105 | unsafe impl Word for u32 {} 106 | unsafe impl Word for u64 {} 107 | unsafe impl Word for u128 {} 108 | 109 | /// Initializes the `.data` section by copying it from the location indicated 110 | /// by `sidata`. 111 | /// 112 | /// # Arguments 113 | /// 114 | /// - `sdata`: Pointer to the start of the `.data` section in RAM. 115 | /// - `edata`: Pointer to the open/non-inclusive end of the `.data` section in 116 | /// RAM (the value behind this pointer will not be modified). 117 | /// - `sidata`: `.data` section Load Memory Address (LMA). Data will be copied 118 | /// from here. 119 | /// - Use `T` to indicate the alignment of the `.data` section and its LMA. 120 | /// 121 | /// # Safety 122 | /// 123 | /// - Must be called exactly once, before the application has started. 124 | /// - `edata >= sdata`. 125 | /// - The `sdata -> edata` region must not overlap with the `sidata -> ...` 126 | /// region. 127 | /// - `sdata`, `edata` and `sidata` must be `T` aligned. 128 | pub unsafe fn init_data(mut sdata: *mut T, edata: *mut T, mut sidata: *const T) 129 | where 130 | T: Word, 131 | { 132 | while sdata < edata { 133 | ptr::write(sdata, ptr::read(sidata)); 134 | sdata = sdata.offset(1); 135 | sidata = sidata.offset(1); 136 | } 137 | 138 | // Ensure that any accesses of `static`s are not reordered before the `.data` section is 139 | // initialized. 140 | // We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before 141 | // *reads*, but this method only *writes* to the locations. 142 | compiler_fence(Ordering::SeqCst); 143 | } 144 | 145 | /// Zeroes the `.bss` section. 146 | /// 147 | /// # Arguments 148 | /// 149 | /// - `sbss`: Pointer to the start of the `.bss` section in RAM. 150 | /// - `ebss`: Pointer to the open/non-inclusive end of the `.bss` section in 151 | /// RAM (the value behind this pointer will not be modified). 152 | /// - Use `T` to indicate the alignment of the `.bss` section. 153 | /// 154 | /// # Safety 155 | /// 156 | /// - Must be called exactly once, before the application has started. 157 | /// - `ebss >= sbss`. 158 | /// - `sbss` and `ebss` must be `T` aligned. 159 | pub unsafe fn zero_bss(mut sbss: *mut T, ebss: *mut T) 160 | where 161 | T: Word, 162 | { 163 | while sbss < ebss { 164 | // NOTE(volatile) to prevent this from being transformed into `memclr` 165 | ptr::write_volatile(sbss, mem::zeroed()); 166 | sbss = sbss.offset(1); 167 | } 168 | 169 | // Ensure that any accesses of `static`s are not reordered before the `.bss` section is 170 | // initialized. 171 | // We use `SeqCst`, because `Acquire` only prevents later accesses from being reordered before 172 | // *reads*, but this method only *writes* to the locations. 173 | compiler_fence(Ordering::SeqCst); 174 | } 175 | -------------------------------------------------------------------------------- /src/test.rs: -------------------------------------------------------------------------------- 1 | use crate::{init_data, zero_bss}; 2 | 3 | #[test] 4 | fn test_init_data() { 5 | let mut data = [0u32, 1, 2, 3, 4, 5, 6, 7]; 6 | let mut mem = [0u32; 8]; 7 | let sdata = data.as_mut_ptr(); 8 | let sidata = mem.as_mut_ptr(); 9 | unsafe { 10 | let edata = sdata.offset(data.len() as isize); 11 | init_data::(sdata, edata, sidata); 12 | }; 13 | 14 | assert_eq!(data, mem); 15 | } 16 | 17 | #[test] 18 | fn test_zero_bss() { 19 | let mut mem = [0xFFFF_FFFFu32; 8]; 20 | let sbss = mem.as_mut_ptr(); 21 | unsafe { 22 | let ebss = sbss.offset(mem.len() as isize); 23 | zero_bss::(sbss, ebss); 24 | }; 25 | 26 | assert_eq!(mem, [0u32; 8]); 27 | } 28 | -------------------------------------------------------------------------------- /triagebot.toml: -------------------------------------------------------------------------------- 1 | [assign] 2 | --------------------------------------------------------------------------------