aux_vec.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //! Part of code and design forked from asterinas.
  2. // SPDX-License-Identifier: MPL-2.0
  3. #![expect(dead_code)]
  4. use alloc::collections::BTreeMap;
  5. use crate::prelude::KResult;
  6. /// Auxiliary Vector.
  7. ///
  8. /// # What is Auxiliary Vector?
  9. ///
  10. /// Here is a concise description of Auxiliary Vector from GNU's manual:
  11. ///
  12. /// > When a program is executed, it receives information from the operating system
  13. /// > about the environment in which it is operating. The form of this information
  14. /// > is a table of key-value pairs, where the keys are from the set of ‘AT_’
  15. /// > values in elf.h.
  16. #[expect(non_camel_case_types)]
  17. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
  18. #[repr(u8)]
  19. pub enum AuxKey {
  20. AT_NULL = 0, /* end of vector */
  21. AT_IGNORE = 1, /* entry should be ignored */
  22. AT_EXECFD = 2, /* file descriptor of program */
  23. AT_PHDR = 3, /* program headers for program */
  24. AT_PHENT = 4, /* size of program header entry */
  25. AT_PHNUM = 5, /* number of program headers */
  26. AT_PAGESZ = 6, /* system page size */
  27. AT_BASE = 7, /* base address of interpreter */
  28. AT_FLAGS = 8, /* flags */
  29. AT_ENTRY = 9, /* entry point of program */
  30. AT_NOTELF = 10, /* program is not ELF */
  31. AT_UID = 11, /* real uid */
  32. AT_EUID = 12, /* effective uid */
  33. AT_GID = 13, /* real gid */
  34. AT_EGID = 14, /* effective gid */
  35. AT_PLATFORM = 15, /* string identifying CPU for optimizations */
  36. AT_HWCAP = 16, /* arch dependent hints at CPU capabilities */
  37. AT_CLKTCK = 17, /* frequency at which times() increments */
  38. /* 18...22 not used */
  39. AT_SECURE = 23, /* secure mode boolean */
  40. AT_BASE_PLATFORM = 24, /* string identifying real platform, may
  41. * differ from AT_PLATFORM. */
  42. AT_RANDOM = 25, /* address of 16 random bytes */
  43. AT_HWCAP2 = 26, /* extension of AT_HWCAP */
  44. /* 28...30 not used */
  45. AT_EXECFN = 31, /* filename of program */
  46. AT_SYSINFO = 32,
  47. AT_SYSINFO_EHDR = 33, /* the start address of the page containing the VDSO */
  48. }
  49. #[derive(Clone, Default, Debug)]
  50. pub struct AuxVec<T> {
  51. table: BTreeMap<AuxKey, T>,
  52. }
  53. impl<T> AuxVec<T> {
  54. pub const fn new() -> AuxVec<T> {
  55. AuxVec {
  56. table: BTreeMap::new(),
  57. }
  58. }
  59. }
  60. impl<T: Clone + Copy> AuxVec<T> {
  61. pub fn set(&mut self, key: AuxKey, val: T) -> KResult<()> {
  62. if key == AuxKey::AT_NULL || key == AuxKey::AT_IGNORE {
  63. panic!("Set illegal key!");
  64. }
  65. self.table
  66. .entry(key)
  67. .and_modify(|val_mut: &mut T| *val_mut = val)
  68. .or_insert(val);
  69. Ok(())
  70. }
  71. pub fn table(&self) -> &BTreeMap<AuxKey, T> {
  72. &self.table
  73. }
  74. }