rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | /*
* vdso.c -- Rustbelt's vDSO ("virtual dynamic shared object", stage 19).
*
* A tiny shared library that the kernel maps into *every* process (it
* isn't a file anywhere: its bytes are built into the kernel). It answers
* "what time is it?" without a system call: a syscall means switching
* into the kernel and back, which costs far more than reading a clock.
*
* How it knows the time: the kernel keeps a read-only page, the "vvar"
* page, just in front of this library's code. In it are the numbers the
* kernel itself uses to turn the CPU's time stamp counter (TSC, read with
* `rdtsc`) into nanoseconds. We do the same arithmetic here, in user
* space, and get exactly the answer the clock_gettime syscall would give.
*
* The kernel may change those numbers while we read them, so they are
* guarded by a *seqlock*: the kernel makes `seq` odd, writes, makes it
* even again. We read `seq`, copy the numbers, read `seq` again: if it was
* odd or has changed, our copy may be half old/half new, so we retry.
*
* When the TSC can't be trusted (it must tick at a constant rate: an
* "invariant TSC"), and for every clock we don't handle, we simply make
* the real system call, so callers always get an answer.
*
* C library calls: musl and glibc look for these functions by name *and
* version* ("LINUX_2.6", see vdso.lds) through the AT_SYSINFO_EHDR entry
* of the auxiliary vector, which points at our ELF header.
*
* Rules for this code: no libc (we *are* below libc), no global writable
* data (the page is shared by every process and read-only), and nothing
* that needs relocating (it can be mapped at any address).
*/
typedef unsigned long u64;
typedef unsigned int u32;
/* Must match `VVar` in src/vdso.rs. */
;
/* The vvar page sits one page below our ELF header (vdso.lds). */
extern const struct vvar vvar_page ;
;
;
;
static inline long
static inline u64
/* Keep the compiler from moving memory reads across this line. */
/*
* Nanoseconds since boot (monotonic) and the boot time, read consistently.
* Returns 0 if the fast path can't be used (then the caller makes the
* syscall). Same formula as `time::now_ns` in src/time.rs.
*/
static int
int
int
long
int
/* The plain names, like Linux's vDSO also offers. */
int ;
int ;
long ;
int ;
|