XenevaOS
Loading...
Searching...
No Matches
kernel.h
Go to the documentation of this file.
1
4#ifndef __LINUX_KERNEL_H__
5#define __LINUX_KERNEL_H__
6
7#include <stdint.h>
8#include <Hal/AA64/aa64cpu.h>
9#include <Mm/kmalloc.h>
10#include <_null.h>
12#include <linux/build_bug.h>
13
14typedef uint8_t u8;
15typedef uint16_t u16;
16typedef uint32_t u32;
17typedef uint64_t u64;
18typedef int8_t s8;
19typedef int16_t s16;
20typedef int32_t s32;
21typedef int64_t s64;
22
23typedef uint8_t __u8;
27typedef int8_t __s8;
28typedef int16_t __s16;
29typedef int32_t __s32;
30typedef int64_t __s64;
31
32typedef uint8_t __le8;
39
44typedef int spinlock_t;
45typedef int mutex;
46
55
56typedef unsigned int gfp_t;
57typedef unsigned int fmode_t;
58typedef unsigned int umode_t;
59/*
60 * irqreturn_t and the IRQ_NONE/IRQ_HANDLED/IRQ_WAKE_THREAD enumerators live
61 * in <linux/irqreturn.h>, which is mainline's byte-port of that header and
62 * spells the type as `enum irqreturn` rather than int. This used to be a
63 * `typedef int irqreturn_t;` here plus three `#define IRQ_*` macros further
64 * down, and both halves collide with the port: the enum members get macro
65 * expansion in their own declaration list ("expected identifier" at
66 * irqreturn.h:12) and the second typedef is a redefinition with different
67 * types. Taking the header keeps the enumerators as enumerators, which is
68 * what makes a handler's return value checkable at all -- with
69 * IRQ_RETVAL(x) as `((x) != IRQ_NONE)` an int answer would compare, but a
70 * typo'd one would too.
71 */
72#include <linux/irqreturn.h>
73typedef unsigned long irq_hw_number_t;
74typedef unsigned long pgoff_t;
75typedef unsigned long kernel_ulong_t;
76
77#include <linux/compiler.h>
78/*
79 * log2.h and math.h sit here because mainline's <linux/kernel.h> includes both
80 * and a ported source reaches is_power_of_2(), roundup_pow_of_two() and the
81 * DIV_ROUND_* family that way, never by naming either header. hexdump.c
82 * (is_power_of_2 at :103), drm_mm.c (roundup_pow_of_two at :1096) and
83 * drm_rect.c (DIV_ROUND_UP_ULL) are the current instances.
84 *
85 * The failure without them is quiet: the call is implicitly declared, which
86 * compiles -- this tree downgrades -Wimplicit-function-declaration -- and the
87 * definition is a header-side static inline that emitted its body into some
88 * other translation unit. The reference survives to the link and stops there.
89 */
90#include <linux/log2.h>
91#include <linux/math.h>
92/*
93 * minmax.h comes before bitmap/list/module so that min/max/min_t/max_t land
94 * in the same place mainline puts them -- mainline's <linux/kernel.h>
95 * includes <linux/minmax.h>, and a ported source reaches min_t() by including
96 * <linux/kernel.h> through its own <linux/...> includes, never by naming
97 * minmax.h itself. virtio-rng.c is the current instance (its copy_data()
98 * writes min_t(unsigned int, ...) twice).
99 *
100 * It is safe this early: minmax.h includes nothing, and every one of its four
101 * definitions is #ifndef-guarded, so anything below that defines its own
102 * min/max (mm.h does, byte-identically) either wins or redefines to the same
103 * tokens -- both legal. ARRAY_SIZE in minmax.h is guarded too, and identical
104 * to the one kernel.h declares further down.
105 */
106#include <linux/minmax.h>
107/*
108 * string.h and sprintf.h: mainline reaches memcpy() and sprintf() from
109 * <linux/kernel.h> too -- memcpy through its device/string chain, sprintf
110 * explicitly (`#include <linux/sprintf.h>`). DCL has no device.h to walk, so
111 * both come in directly. A ported source never names either header itself:
112 * virtio-rng.c calls memcpy() at :94 and sprintf() at :165 while including
113 * only <linux/err.h> and <linux/virtio.h>.
114 *
115 * Without these two the calls are implicitly declared, which still *links*
116 * (implicit declarations are allowed here) and produces no warning by
117 * default -- that is the failure mode: sprintf's return type happens to be
118 * int, so the compiler has nothing to complain about until the day a call
119 * passes a mismatched pointer, at which point it would have been checking an
120 * assumption rather than a declaration.
121 */
122#include <linux/string.h>
123#include <linux/sprintf.h>
124#include <linux/bitmap.h>
125#include <linux/list.h>
126#include <linux/module.h>
127#include <linux/printk.h>
128#include <linux/timer.h>
129#include <linux/workqueue.h>
130#include <linux/usb/ch9.h>
131#include <linux/usb/otg.h>
132#include <linux/virtio.h>
133
134#define udelay(us) AA64SleepUS(us)
135#define mdelay(ms) AA64SleepMS(ms)
136#define msleep(ms) mdelay(ms)
137#define usleep(us) AA64SleepUS(us)
138
139#define GFP_KERNEL 0
140#define GFP_ATOMIC 1
141#define GFP_DMA 2
142
143/*
144 * The two xarray.c names that are not among those three. lib/xarray.c
145 * includes <linux/slab.h>, and mainline's slab.h pulls <linux/gfp.h> in for
146 * exactly these; DCL's does not, so they live beside the rest of the gfp
147 * surface here rather than in a header xarray never reads.
148 *
149 * GFP_NOWAIT is "allocate without waiting", which in DCL's two-value scheme
150 * is the same answer GFP_ATOMIC already gives: the allocator neither blocks
151 * nor refuses, so the flag changes no path -- it has to *parse*, and 1 says
152 * what the caller meant. __GFP_ACCOUNT is memcg accounting, which this tree
153 * does not have; 0 is not a placeholder but the whole of what accounting is
154 * worth here, and it is written as a flag-shaped 0 so that
155 * `GFP_NOWAIT | __GFP_ACCOUNT` (xarray.c:308, :338, :376) folds to GFP_NOWAIT
156 * instead of to an undeclared identifier.
157 */
158#define GFP_NOWAIT 1
159#define __GFP_ACCOUNT 0
160
161/*
162 * __GFP_BITS_SHIFT -- first bit above the gfp word, which is where xarray
163 * keeps its three mark bits (XA_FLAGS_MARK, xarray.h:277). mainline takes
164 * it from the end of gfp_types.h's flag list; DCL's flags are the three
165 * sentinels above, and the xa_flags low bits (4/8/16/32) claim 2..5, so 26
166 * is mainline's clear high ground rather than a next-to-nothing guess.
167 */
168#define __GFP_BITS_SHIFT 26
169
170#define container_of(ptr, type, member) \
171 ((type*)((char*)(ptr)- offsetof(type,member)))
172
173#ifndef offsetof
174#if defined(__GNUC__) || defined(__clang__)
175#define offsetof(type, member) __builtin_offsetof(type, member)
176#else
177#define offsetof(type, member) \
178 ((size_t)&((type*)0)->member)
179#endif
180#endif
181
182/*
183 * IS_ENABLED(), IS_BUILTIN(), IS_MODULE(), IS_REACHABLE() and the CONFIG_
184 * table they read are <linux/kconfig.h>'s -- mainline's kernel.h defines none
185 * of them. They are reached here rather than in kconfig.h's own callers
186 * because this is the include DCL's drivers already carry, so hanging it on
187 * the header they all take is what keeps the table in scope wherever the
188 * macro is. Without that the macro silently reads an undeclared identifier
189 * and the error lands in the *driver*, three includes away, as "use of
190 * undeclared identifier 'CONFIG_SERIAL_8250_CONSOLE'" at 8250_port.c:3197
191 * while the definition sits correctly in autoconf.h:42.
192 *
193 * kconfig.h pulls <linux/autoconf.h> in itself (mainline: generated/
194 * autoconf.h) and asks nothing else of the tree, so it cannot cycle.
195 *
196 * What goes with the old `!!(opt)` spelling is the rule it forced:
197 * "define every option, even at 0". mainline's token paste never names the
198 * identifier, so an absent CONFIG_* reads as 0 in a #if *and* in a function
199 * body -- while an option present at 0 is still present to an #ifdef, which
200 * is how a defined-but-false option would have answered #ifdef CONFIG_CPU_BIG
201 * _ENDIAN with "yes". autoconf.h keeps only what is on, mainline's rule.
202 */
203#include <linux/kconfig.h>
204
205/*
206 * might_sleep() -- "this function may schedule, so do not call it from a
207 * context that cannot". tty_ldsem.c puts one on every ldsem acquisition,
208 * because a contended semaphore really does park.
209 *
210 * mainline's is a real check when CONFIG_DEBUG_ATOMIC_SLEEP is on (it
211 * complains if irqs are disabled) and might_resched() otherwise; the
212 * preprocessor spelling `# define might_sleep() ...` in
213 * include/linux/kernel.h:90 is the no-debug arm, and empty is what DCL
214 * matches -- there is no atomic-context bookkeeping to compare against.
215 * It has to be a macro, not a function: it is called with no arguments at
216 * all, so a declaration would have to be `void might_sleep()` (a call, not a
217 * definition) and would have left the body to a link error.
218 *
219 * The annotation is honest in the other direction too: DCL's schedule() is
220 * mdelay(1), so sleeping here is always bounded, and nothing in this build
221 * can be in an atomic section it did not choose.
222 */
223#define might_sleep() do {} while (0)
224
225#define spin_lock_init(l) do{} while(0)
226#define spin_lock_irqsave(l,f) do{(f) = 0;}while(0)
227#define spin_unlock_irqrestore(l, f) do{}while(0)
228#define mutex_init(m) do{} while(0)
229#define mutex_lock(m) do{}while(0)
230#define mutex_unlock(m) do{} while(0)
231
232/* IRQ_NONE / IRQ_HANDLED / IRQ_WAKE_THREAD were defined here as 0/1/2 and
233 * are now <linux/irqreturn.h>'s enumerators, included with the typedef
234 * above. See the note there. */
235
236#ifndef __iomem
237#define __iomem
238#endif
239#ifndef __user
240#define __user
241#endif
242#ifndef __kernel
243#define __kernel
244#endif
245#ifndef __force
246#define __force
247#endif
248#ifndef __nocast
249#define __nocast
250#endif
251#ifndef __safe
252#define __safe
253#endif
254/*
255 * __aligned(x) is NOT an empty annotation, unlike the ones around it.
256 * struct tty_buffer ends in `u8 data[] __aligned(sizeof(unsigned long))`
257 * (tty_buffer.h:47), and that alignment is load-bearing rather than cosmetic:
258 * the buffer is allocated as one block and the flag array is found by adding
259 * b->size bytes, so an under-aligned `data[]` would put every flag byte -- and
260 * every subsequent flag_buf_ptr() read -- at a wrong offset relative to what
261 * the store used. Xeneva's allocator hands back word-aligned memory anyway,
262 * but the attribute states the requirement instead of relying on it.
263 */
264#ifndef __aligned
265#define __aligned(x) __attribute__((aligned(x)))
266#endif
267#define __chk_user_ptr(x) (void)0
268#define __chk_io_ptr(x) (void)0
269
270#define EPERM 1
271#define ENOENT 2
272#define EIO 5
273#define ENOMEM 12
274#define EBUSY 16
275/* Two errno numbers serial_core.c names that DCL's set did not have. Same
276 * numbering as mainline's asm-generic/errno.h, because these are visible to
277 * userspace through the value write() returns -- the number *is* the
278 * interface, not an internal choice.
279 *
280 * ENXIO 6 "no such device or address": uart_tiocmget's failed lookup
281 * at serial_core.c:1940, uart_ioctl's at :2987.
282 * EL3HLT 46 the tty layer's "hangup requested" answer, which
283 * uart_hangup() compares against at serial_core.c:618. -46 is
284 * not a failure there; it is the recognised "this port was hung
285 * up underneath you" result the caller switches on.
286 */
287#define ENXIO 6
288#define E2BIG 7
289#define EPROBE_DEFER 517
290#define EL3HLT 46
291#define ENODEV 19
292#define EINVAL 22
293#define ENOSPC 28
294#define ETIMEDOUT 110
295#define ENOTSUPP 524
296/* Kernel-internal restart code -- not a userspace errno. <linux/mm.h>
297 * defines it too, under #ifndef, so whichever header loads first wins and
298 * both agree on 512. It belongs in the errno block because
299 * scoped_cond_guard(mutex_intr, return -ERESTARTSYS, ...) in serial_core.c
300 * needs it visible from <linux/mutex.h> alone, without mm.h. */
301#ifndef ERESTARTSYS
302#define ERESTARTSYS 512
303#endif
304#define MAX_ERRNO 4095
305#define IS_ERR_VALUE(x) ((x) >= (uintptr_t)-MAX_ERRNO)
306
307#define EDEADLK 35
308#define ENAMETOOLONG 36
309#define ENOLCK 37
310#define ENOSYS 38
311#define ENOTEMPTY 39
312#define ELOOP 40
313#define EAGAIN 11
314#define EWOULDBLOCK EAGAIN
315#define ENOMSG 42
316#define EIDRM 43
317#define ENOSTR 60
318#define ENODATA 61
319#define ETIME 62
320#define ENOSR 63
321#define EREMOTE 66
322#define ENOLINK 67
323#define EPROTO 71
324#define EMULTIHOP 72
325#define EBADMSG 74
326#define EOVERFLOW 75
327#define EILSEQ 84
328#define EUSERS 87
329#define ENOTSOCK 88
330#define EDESTADDRREQ 89
331#define EMSGSIZE 90
332#define ENOPROTOOPT 92
333#define EPROTONOSUPPORT 93
334#define ESOCKTNOSUPPORT 94
335#define EOPNOTSUPP 95
336#define EAFNOSUPPORT 97
337#define EADDRINUSE 98
338#define EADDRNOTAVAIL 99
339#define ENETDOWN 100
340#define ENETUNREACH 101
341#define ENETRESET 102
342#define ECONNABORTED 103
343#define ECONNRESET 104
344#define ENOBUFS 105
345#define EISCONN 106
346#define ENOTCONN 107
347#define ESHUTDOWN 108
348#define ETOOMANYREGS 109
349#define ECONNREFUSED 111
350#define EHOSTDOWN 112
351
352#define readb(addr) (*(volatile u8*)((uint8_t*)addr))
353#define readw(addr) (*(volatile u16*)((uint8_t*)addr))
354#define readl(addr) (*(volatile u32*)((uint8_t*)addr))
355#define writeb(v, addr) (*(volatile u8*)((uint8_t*)addr) = (v))
356#define writew(v, addr) (*(volatile u16*)((uint8_t*)addr) = (v))
357#define writel(v, addr) (*(volatile u32*)((uint8_t*)addr) = (v))
358
359#define BITS_PER_BYTE 8
360
361/*
362 * BITS_PER_LONG -- bits in an `unsigned long`, which is NOT the same as bits
363 * in a pointer on this target.
364 *
365 * aarch64-unknown-windows is LLP64: __SIZEOF_LONG__ is 4 while
366 * __SIZEOF_SIZE_T__ and pointers are 8. This was hardcoded to 64, and every
367 * consumer of it inherits the mistake:
368 *
369 * - bitops.h's test_bit/set_bit compute the word index as (nr)/BITS_PER_LONG
370 * and the shift as (nr)%BITS_PER_LONG. With 64, bits 32..63 land in word 0
371 * instead of word 1, and `1UL << 32..63` is a shift by >= the width of a
372 * 32-bit type, which is undefined. Nothing hit it yet only because
373 * tty_struct_flags runs 0..11 and TTY_PORT_* runs 0..5, so every index is
374 * < 32 and the two spellings happen to agree.
375 * - GENMASK(h,l) shifts `~0UL` -- a 32-bit value -- by BITS_PER_LONG-1-h,
376 * i.e. by 62-h with h small: also undefined, and in practice a 0.
377 *
378 * It is computed rather than typed as 32 so it stays true if the target ever
379 * changes; this is exactly how BaseHdr/bordoisila_bits.h spells it, and that
380 * header's GENMASK had to be correct for the iMX8MP PLL register fields.
381 */
382/*
383 * 32, not (sizeof(long) * BITS_PER_BYTE): same number, but a number a #if can
384 * read. tty_ldsem.c:36 does `#if BITS_PER_LONG == 64` and rejected the
385 * expression form outright ("function-like macro 'sizeof' is not defined").
386 *
387 * It is genuinely 32 here and not a guess -- this is aarch64 under the LLP64
388 * ABI, where long is 4 bytes -- and 32 is what the reader wants: the only
389 * #if consumer sizes the active-count mask in struct ld_semaphore against
390 * atomic_long_t, whose counter is a long (atomic.h:141), so the word and the
391 * mask have to agree on 32 and the 64-bit arm would not.
392 *
393 * mainline's value on aarch64 is 64 because its long is 64 there; nothing
394 * about that portability transfers, since DCL's `long` is not the machine
395 * word. Consumers that want the machine word spell it as size_t.
396 */
397#define BITS_PER_LONG 32
398
399#define BIT(n) (1UL << (n))
400#define BIT_ULL(n) (1ULL << (n))
401#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG-1-(h))))
402#define GENMASK_ULL(h,l) (((~0ULL) << (l)) & (~0ULL >> (63-(h))))
403
405
406#define NSEC_PER_USEC 1000LL
407#define NSEC_PER_MSEC 1000000LL
408#define NSEC_PER_SEC 1000000000LL
409#define USEC_PER_MSEC 1000LL
410#define USEC_PER_SEC 1000000LL
411#define MSEC_PER_SEC 1000LL
412
413#define KTIME_MAX ((ktime_t)~((uint64_t)1<<63))
414#define KTIME_MIN (~KTIME_MAX - 1)
415#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
416
417static inline ktime_t ktime_set(long secs, unsigned long nsecs) {
418 return (ktime_t)secs * NSEC_PER_SEC + (ktime_t)nsecs;
419}
420
421static inline ktime_t ktime_add(ktime_t a, ktime_t b) {
422 return a + b;
423}
424
425static inline ktime_t ktime_sub(ktime_t a, ktime_t b) {
426 return a - b;
427}
428
429static inline ktime_t ktime_add_ns(ktime_t a, uint64_t ns) {
430 return a + (ktime_t)ns;
431}
432
433static inline ktime_t ktime_sub_ns(ktime_t a, uint64_t ns) {
434 return a - (ktime_t)ns;
435}
436
437static inline ktime_t ktime_add_us(ktime_t a, uint64_t us) {
438 return a + (ktime_t)(us * NSEC_PER_USEC);
439}
440
441static inline ktime_t ktime_add_ms(ktime_t a, uint64_t ms) {
442 return a + (ktime_t)(ms * NSEC_PER_MSEC);
443}
444
445static inline int ktime_compare(ktime_t a, ktime_t b) {
446 if (a < b) return -1;
447 if (a > b) return 1;
448 return 0;
449}
450
451static inline bool ktime_after(ktime_t a, ktime_t b) {
452 return a > b;
453}
454
455static inline bool ktime_before(ktime_t a, ktime_t b) {
456 return a < b;
457}
458
459static inline int64_t ktime_to_ns(ktime_t kt) {
460 return (int64_t)kt;
461}
462
463static inline int64_t ktime_to_us(ktime_t kt) {
464 return (int64_t)kt / NSEC_PER_USEC;
465}
466
467static inline int64_t ktime_to_ms(ktime_t kt) {
468 return (int64_t)kt / NSEC_PER_MSEC;
469}
470
471static inline int64_t ktime_to_s(ktime_t kt) {
472 return (int64_t)kt / NSEC_PER_SEC;
473}
474
475static inline ktime_t ns_to_ktime(uint64_t ns) {
476 return (ktime_t)ns;
477}
478
479static inline ktime_t us_to_ktime(uint64_t us) {
480 return (ktime_t)(us * NSEC_PER_USEC);
481}
482
483static inline ktime_t ms_to_ktime(uint64_t ms) {
484 return (ktime_t)(ms * NSEC_PER_MSEC);
485}
486
487static inline ktime_t ktime_sub_us(ktime_t a, uint64_t us) {
488 return a - (ktime_t)(us * NSEC_PER_USEC);
489}
490
491static inline ktime_t ktime_us_delta(ktime_t later, ktime_t earlier) {
492 return ktime_to_us(ktime_sub(later, earlier));
493}
494
495static inline int64_t ktime_ms_delta(ktime_t later, ktime_t earlier) {
496 return ktime_to_ms(ktime_sub(later, earlier));
497}
498
499#define TK_OFFS_BOOT 0
500static inline ktime_t ktime_mono_to_real(ktime_t t) { return t; }
501static inline ktime_t ktime_mono_to_any(ktime_t t, int offs) { (void)offs; return t; }
502
503static inline ktime_t ktime_get(void) {
504 uint64_t cnt = get_cntpct_el0();
505 uint64_t freq = get_cntfrq_el0();
506
507 return (ktime_t)(cnt / (freq / NSEC_PER_SEC));
508}
509
510static inline ktime_t ktime_get_mono_fast_ns(void) {
511 return ktime_get();
512}
513
514/*
515 * struct timespec64 / ktime_to_timespec64() -- the calendar split of a
516 * ktime_t. mainline has both in <linux/ktime.h>; they live here because
517 * this is where DCL keeps ktime_t and its arithmetic, and drm_vblank.c's
518 * timestamp paths (vblank.c:818/1057/1730) reach this header first. The
519 * nanosecond field is `long long` rather than mainline's `long` because
520 * this target is LLP64 and `long` is 4 bytes -- tv_nsec only ever holds a
521 * sub-second remainder, but a type that is right by coincidence is a trap
522 * the first time someone adds tv_nsec arithmetic.
523 */
526 long long tv_nsec;
527};
528
529static inline struct timespec64 ktime_to_timespec64(const ktime_t kt) {
530 struct timespec64 ts;
531 ts.tv_sec = (int64_t)(kt / NSEC_PER_SEC);
532 ts.tv_nsec = (long long)(kt % NSEC_PER_SEC);
533 return ts;
534}
535
536static inline uint64_t ktime_get_ns(void) {
537 return (uint64_t)ktime_get();
538}
539
540#if defined(_MSC_VER)
541/*
542 * This branch was missing its `: <expression>` -- it read
543 * `((cond) ? (UARTDebugOut(...)))`, a ternary with no colon, so every use of
544 * WARN_ON() was a hard parse error rather than a warning. It never fired in
545 * stage 1 because nothing in that stage expanded WARN_ON; tty_buffer.c:210 is
546 * the first call site, and the target is `aarch64-unknown-windows`, which
547 * defines _MSC_VER -- so this branch, not the clang one below, is the one DCL
548 * actually builds with. The comma form also fixes the second problem:
549 * UARTDebugOut() returns void (Drivers/uart.h:91), so a plain
550 * `cond ? void : 0` would have been a type error even with the colon.
551 */
552#define WARN_ON(cond) \
553 ((cond) ? (UARTDebugOut("[WARN] %s:%d %s \r\n", \
554 __FILE__, __LINE__, #cond), 1) : 0)
555#elif defined(__GNUC__) && defined(__clang__)
556#define WARN_ON(cond) ({ \
557int __c = !!(cond); \
558if (__c){ \
559UARTDebugOut("[WARN] %s:%d \r\n", __FILE__, __LINE__); \
560} \
561})
562#endif
563
564/* Mainline's stack-banner helper. DCL's input core calls it only on an
565 * invalid event type; there is no unwinder, so the sink is silent. */
566void dump_stack(void);
567
568#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
569
570#ifndef DIV_ROUND_UP
571#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
572#endif
573#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
574#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
575#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
576#define MIN(a, b) ((a) < (b) ? (a) : (b))
577#define MAX(a, b) ((a) > (b) ? (a) : (b))
578#define clamp(val, lo, hi) ((val) < (lo) ? (lo) : ((val) > (hi) ? (hi) : (val)))
579#define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
580#define upper_32_bits(n) ((uint32_t)((n) >> 32))
581#define lower_32_bits(n) ((uint32_t)((n) & 0xFFFFFFFF))
582
583/*
584 * ------------------------------------------------------------------------
585 * Access and barrier primitives (added for the tty/serial port)
586 * ------------------------------------------------------------------------
587 *
588 * READ_ONCE/WRITE_ONCE are the "one access, no compiler cleverness" pair.
589 * The tty code needs them structurally rather than for performance:
590 * tty_buffer.c:64 reads buf->flip_wq with READ_ONCE() before handing it to a
591 * work function, and the acquire/release forms around commit/next (lines 489,
592 * 522, 290, 296) are what stop the producer's store of the byte *before* the
593 * cursor from being observed after the cursor itself. On a uniprocessor with
594 * no compiler reordering across a volatile access that would already hold --
595 * but volatile is not an ordering primitive, so these are built on __atomic
596 * with the right ordering instead of on volatile casts. The distinction costs
597 * nothing and keeps the header honest if DCL ever runs on more than one core.
598 *
599 * smp_load_acquire/smp_store_release take pointers of any type, which is why
600 * they are macros over __atomic_load_n/__atomic_store_n rather than
601 * typed inlines: they are called on both unsigned int* (head->commit) and
602 * struct tty_buffer** (head->next).
603 */
604#ifndef READ_ONCE
605#define READ_ONCE(x) __atomic_load_n(&(x), __ATOMIC_RELAXED)
606#endif
607#ifndef WRITE_ONCE
608#define WRITE_ONCE(x, val) __atomic_store_n(&(x), (val), __ATOMIC_RELAXED)
609#endif
610#ifndef smp_load_acquire
611#define smp_load_acquire(p) __atomic_load_n((p), __ATOMIC_ACQUIRE)
612#endif
613#ifndef smp_store_release
614#define smp_store_release(p, v) __atomic_store_n((p), (v), __ATOMIC_RELEASE)
615#endif
616#ifndef smp_mb
617#define smp_mb() __atomic_thread_fence(__ATOMIC_SEQ_CST)
618#endif
619#ifndef smp_rmb
620#define smp_rmb() __atomic_thread_fence(__ATOMIC_ACQUIRE)
621#endif
622#ifndef smp_wmb
623#define smp_wmb() __atomic_thread_fence(__ATOMIC_RELEASE)
624#endif
625
626/*
627 * WARN(cond, fmt, ...) -- mainline's "report and continue" that is *not*
628 * counted as an error (unlike BUG). tty_buffer.c:150 uses it to say a memory
629 * accounting mismatch survived tty_buffer_free_all(); the allocation was
630 * already freed, so the only useful response is to say so and carry on.
631 *
632 * WARN_ON exists above (both the MSVC and clang spellings). This adds the
633 * printf form and WARN_ON_ONCE, which is the form used on paths that can run
634 * once per received byte -- printing per byte would be worse than silence.
635 */
636#ifndef WARN
637#define WARN(cond, fmt, ...) \
638 ({ \
639 int __wret = !!(cond); \
640 if (__wret) \
641 UARTDebugOut("[WARN] " fmt " (%s:%d)\r\n", \
642 ##__VA_ARGS__, __FILE__, __LINE__); \
643 __wret; \
644 })
645#endif
646
647#ifndef WARN_ON_ONCE
648#define WARN_ON_ONCE(cond) \
649 ({ \
650 static int __warned_once; \
651 int __wret = !!(cond); \
652 if (__wret && !__warned_once) { \
653 __warned_once = 1; \
654 UARTDebugOut("[WARN] %s:%d\r\n", \
655 __FILE__, __LINE__); \
656 } \
657 __wret; \
658 })
659#endif
660
661/*
662 * __randomize_layout -- a GCC plugin attribute that shuffles struct field
663 * order per build. DCL has no plugin and no threat model that needs one; the
664 * ported tty structs simply drop it. Defined as nothing so a file that
665 * carries it still parses.
666 */
667#ifndef __randomize_layout
668#define __randomize_layout
669#endif
670
671
672/*
673 * xchg() -- atomic exchange.
674 *
675 * Not a DCL invention: __atomic_exchange_n with sequential consistency is the
676 * same guarantee mainline's arch-neutral xchg() asks for, and this target's
677 * clang lowers it to a single ldaxr/stlxr pair rather than a lock. Two call
678 * sites, both in 8250_port.c, both swapping a *pointer* (the active RS485
679 * timer under the port lock), which is the case where an int-sized
680 * "exchange" would have been silent: the write would land with a truncated
681 * address.
682 */
683#define xchg(ptr, v) __atomic_exchange_n((ptr), (v), __ATOMIC_SEQ_CST)
684
685/*
686 * oops_in_progress -- nonzero while the kernel is in a fault handler.
687 *
688 * mainline declares it in <linux/oops.h>; DCL hangs it off printk.h because
689 * that is who reads it: serial8250_console_write() at 8250_port.c:3341 takes
690 * the port lock only when this is clear, on the reasoning that during a fault
691 * the lock may already be held by the CPU that is reporting, and taking it
692 * then would deadlock *inside* the crash dump.
693 *
694 * It is a real variable rather than `#define oops_in_progress 0` for the same
695 * reason: panic() below sets it, and a macro could not be. Defined in
696 * DCL/linux_irq_shim.c, next to the console registry it guards.
697 */
698extern int oops_in_progress;
699
700/* kstrtou8 and friends -- see that header for why they are hand-rolled. */
701#include <linux/kstrtox.h>
702
703/*
704 * BUG() / BUG_ON() -- mainline spells these in <linux/bug.h>, which
705 * serial_core.c does not include; <linux/kernel.h> is where its own copies
706 * live upstream too (via linux/bug.h), and it is the header every file in
707 * the tree reaches, so it is where these go.
708 *
709 * DCL has no die(), no exception table and no oops-then-continue: BUG() is
710 * panic(), which sets oops_in_progress, prints and stops. That is the same
711 * observable behaviour mainline gives a BUG() -- the machine does not carry
712 * on with a corrupted invariant -- and the two call sites agree that it
713 * should stop:
714 *
715 * serial_core.c:120 BUG_ON(!state) uart_write_wakeup() with no
716 * state means the tty layer has a
717 * port that was never set up; the
718 * next line dereferences it.
719 * serial_core.c:2731 BUG_ON(drv->state) registering the same uart_driver
720 * twice would double every list.
721 *
722 * The alternative -- a no-op, which is what mainline compiles BUG_ON down to
723 * when CONFIG_BUG=n -- would turn both into a null dereference a few
724 * instructions later, with a fault instead of a message naming the line that
725 * fired.
726 */
727#define BUG() \
728 panic("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__)
729
730#define BUG_ON(condition) do { if (condition) { BUG(); } } while (0)
731
732/*
733 * sized_strscpy(dst, src, count) -- copy a NUL-terminated string into a
734 * bounded buffer. The result is discarded by every caller in the tree, but
735 * the contract still has to be mainline's, because it is the *return* that
736 * tells a future caller whether the string fit:
737 *
738 * >= 0 bytes copied, excluding the terminator
739 * -E2BIG src did not fit in count bytes; dst is still NUL-terminated
740 *
741 * Byte loop rather than memcpy, and deliberately so: the terminator is
742 * written as part of the copy, so there is no window in which dst holds an
743 * unterminated run -- which is the whole point of strscpy against strncpy,
744 * whose mainline comment says the same thing in more words. count == 0 is
745 * -E2BIG because nothing could be stored, matching upstream.
746 *
747 * The name is mainline's for this spelling: mainline v7.2 has the 3-argument
748 * form here and builds strscpy() on top of it -- see the macros below.
749 *
750 * mainline: include/linux/string.h -> lib/string.c; see the note above this
751 * block for why the definition sits in this file instead.
752 */
753static inline long sized_strscpy(char* dst, const char* src, size_t count)
754{
755 size_t i;
756
757 if (count == 0)
758 return -E2BIG;
759
760 for (i = 0; i + 1 < count && src[i]; i++)
761 dst[i] = src[i];
762
763 if (src[i])
764 {
765 dst[0] = '\0';
766 return -E2BIG;
767 }
768
769 dst[i] = '\0';
770 return (long)i;
771}
772
773/*
774 * strscpy(dst, src[, count]) -- mainline's two-form spelling of the above.
775 *
776 * mainline dispatches on the argument count rather than overloading, because
777 * C cannot overload and this tree has both shapes in it at once:
778 *
779 * virtio_console.c:1635 strscpy(port->name, ..., sizeof(port->name));
780 * serial_core.c:2485 strscpy(address, "*unknown*", sizeof(address));
781 * drm_client.c:253 strscpy(fb->comm, client->name, TASK_COMM_LEN);
782 * drm_modes.c:2322 strscpy(cmdline_mode->name, mode->name,
783 * sizeof(cmdline_mode->name));
784 * drm_framebuffer.c:886 strscpy(fb->comm, current->comm);
785 *
786 * The last line is what made this necessary. Against the plain 3-argument
787 * function this file used to carry, it reported "too few arguments to
788 * function call, expected 3, have 2" -- and adding a default parameter was
789 * not the fix, because mainline's rule is that the 2-argument form is only
790 * valid when dst *is* an array whose size is known, and the count then comes
791 * from sizeof(dst). The four 3-argument sites pass a count that is not the
792 * whole array (a TASK_COMM_LEN into a longer field), which is the other half
793 * of why the argument cannot simply be dropped.
794 *
795 * The dispatch is mainline's in shape and different in mechanism, and the
796 * difference is worth recording: mainline selects with COUNT_ARGS() over a
797 * padded argument list, which needs the __ARG_PLACEHOLDER machinery for the
798 * empty case. Checked rather than assumed, clang counts an empty __VA_ARGS__
799 * as *one* empty argument, so the padded-list version selects the 3-argument
800 * arm for a 2-argument call and then complains that its size argument is
801 * missing.
802 *
803 * __VA_OPT__ is direct about the same question. __strscpy_sel returns its
804 * middle argument; the first __VA_OPT__ supplies "the count, then the
805 * 3-argument arm" only when a count was actually written, and when it was
806 * not, the slot that __VA_OPT__ left empty is precisely the middle one --
807 * which lands on __strscpy_2. __strscpy_2 is variadic so the trailing empty
808 * __VA_ARGS__ that strscpy() passes has somewhere to go.
809 *
810 * Both arms are always parsed, so dst is sizeof'd even on the 3-argument
811 * path; that is harmless and is the price of picking the arm after expansion
812 * rather than before.
813 */
814#define __strscpy_2(dst, src, ...) sized_strscpy(dst, src, sizeof(dst))
815#define __strscpy_3(dst, src, size) sized_strscpy(dst, src, size)
816#define __strscpy_sel(_1, N, ...) N
817#define __strscpy_args(...) __strscpy_sel(__VA_OPT__(__VA_ARGS__, __strscpy_3) , __strscpy_2)
818#define strscpy(dst, src, ...) __strscpy_args(__VA_ARGS__)(dst, src, __VA_ARGS__)
819#endif
820
unsigned int uintptr_t
Definition stdint.h:94
XETime t
Definition main.cpp:53
size_t a
Definition main.cpp:156
size_t b
Definition main.cpp:156
AU_EXTERN AU_EXPORT uint64_t get_cntpct_el0()
AU_EXTERN AU_EXPORT uint64_t get_cntfrq_el0()
int int32_t
Definition acefiex.h:160
unsigned int uint32_t
Definition acefiex.h:163
COMPILER_DEPENDENT_INT64 int64_t
Definition acefiex.h:164
short int int16_t
Definition acefiex.h:159
unsigned char uint8_t
Definition acefiex.h:161
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
unsigned short int uint16_t
Definition acefiex.h:162
signed char int8_t
Definition acefiex.h:158
#define NSEC_PER_SEC
Definition kernel.h:408
uintptr_t phys_addr_t
Definition kernel.h:40
int8_t __s8
Definition kernel.h:27
uint32_t __be32
Definition kernel.h:37
#define NSEC_PER_USEC
Definition kernel.h:406
uint64_t blkcnt_t
Definition kernel.h:48
int64_t s64
Definition kernel.h:21
int32_t __s32
Definition kernel.h:29
uint32_t __u32
Definition kernel.h:25
uint64_t u64
Definition kernel.h:17
uint32_t dev_t
Definition kernel.h:52
void dump_stack(void)
int64_t __s64
Definition kernel.h:30
unsigned long irq_hw_number_t
Definition kernel.h:73
uint64_t __be64
Definition kernel.h:38
int64_t ktime_t
Definition kernel.h:404
uintptr_t dma_addr_t
Definition kernel.h:42
int oops_in_progress
uint8_t __u8
Definition kernel.h:23
uint64_t loff_t
Definition kernel.h:49
uintptr_t resource_size_t
Definition kernel.h:41
unsigned long pgoff_t
Definition kernel.h:74
uint16_t __le16
Definition kernel.h:33
uint8_t u8
Definition kernel.h:14
uint64_t __le64
Definition kernel.h:35
uint16_t __be16
Definition kernel.h:36
unsigned int gfp_t
Definition kernel.h:56
unsigned long kernel_ulong_t
Definition kernel.h:75
int16_t __s16
Definition kernel.h:28
int8_t s8
Definition kernel.h:18
uint32_t nlink_t
Definition kernel.h:54
uint32_t gid_t
Definition kernel.h:51
int16_t s16
Definition kernel.h:19
#define E2BIG
Definition kernel.h:288
uint16_t __u16
Definition kernel.h:24
uint64_t __u64
Definition kernel.h:26
uint16_t u16
Definition kernel.h:15
#define NSEC_PER_MSEC
Definition kernel.h:407
uint32_t __le32
Definition kernel.h:34
int spinlock_t
Definition kernel.h:44
int32_t s32
Definition kernel.h:20
uint64_t sector_t
Definition kernel.h:47
unsigned int fmode_t
Definition kernel.h:57
unsigned int umode_t
Definition kernel.h:58
uint32_t ino_t
Definition kernel.h:53
int mutex
Definition kernel.h:45
uint32_t uid_t
Definition kernel.h:50
uint8_t __le8
Definition kernel.h:32
uint32_t u32
Definition kernel.h:16
uintptr_t io_addr_t
Definition kernel.h:43
Definition kernel.h:524
long long tv_nsec
Definition kernel.h:526
int64_t tv_sec
Definition kernel.h:525