1/* @title: Type aliases */
2#pragma once
3#include <atomic.h>
4#include <compiler/core.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include <stdint.h>
8
9/* Time units */
10ct_strong_int(time_ns, TIME_NS, uint64_t, UINT64_MAX);
11ct_strong_int(time_us, TIME_US, uint64_t, UINT64_MAX);
12ct_strong_int(time_ms, TIME_MS, uint64_t, UINT64_MAX);
13ct_strong_int(time_s, TIME_S, uint64_t, UINT64_MAX);
14typedef uint64_t timestamp_t;
15
16/* Frequency */
17ct_strong_int(freq_hz, FREQ_HZ, uint64_t, UINT64_MAX);
18ct_strong_int(freq_khz, FREQ_KHZ, uint64_t, UINT64_MAX);
19ct_strong_int(freq_mhz, FREQ_MHZ, uint64_t, UINT64_MAX);
20ct_strong_int(freq_ghz, FREQ_GHZ, uint64_t, UINT64_MAX);
21
22/* Size (binary/IEC units, powers of 1024) */
23ct_strong_int(sz_kib, SZ_KIB, size_t, SIZE_MAX);
24ct_strong_int(sz_mib, SZ_MIB, size_t, SIZE_MAX);
25ct_strong_int(sz_gib, SZ_GIB, size_t, SIZE_MAX);
26ct_strong_int(sz_tib, SZ_TIB, size_t, SIZE_MAX);
27
28/* Size (decimal/SI units, powers of 1000) */
29ct_strong_int(sz_kb, SZ_KB, size_t, SIZE_MAX);
30ct_strong_int(sz_mb, SZ_MB, size_t, SIZE_MAX);
31ct_strong_int(sz_gb, SZ_GB, size_t, SIZE_MAX);
32ct_strong_int(sz_tb, SZ_TB, size_t, SIZE_MAX);
33
34/* Bytes */
35ct_strong_int(sz_b, SZ_B, size_t, SIZE_MAX);
36
37/* Aliases over UNIX identifiers */
38ct_strong_int(inode, INODE, uint32_t, UINT32_MAX);
39typedef uint16_t mode_t;
40typedef uint32_t gid_t;
41typedef uint32_t uid_t;
42
43/* For stack depot: someday will become uint32_t */
44typedef struct stack_depot_record *stack_handle_t;
45
46/* Refcount */
47typedef atomic_uint32_t refcount_t;
48typedef atomic_uint32_t mapcount_t;
49
50/* Addresses and memory */
51ct_strong_int(paddr, PADDR, uintptr_t, UINTPTR_MAX);
52ct_strong_int(vaddr, VADDR, uintptr_t, UINTPTR_MAX);
53ct_strong_int(pfn, PFN, uintptr_t, UINTPTR_MAX);
54ct_strong_int(pgoff, PGOFF, uintptr_t, UINTPTR_MAX);
55ct_strong_int(iova, IOVA, uintptr_t, UINTPTR_MAX);
56ct_strong_int(pte, PTE, uint64_t, UINT64_MAX);
57typedef uint64_t page_flags_t;
58
59/* Processor topology and threading */
60typedef uint8_t irq_t;
61typedef int8_t cpu_perf_t;
62ct_strong_int(cpu_id, CPU_ID, size_t, SIZE_MAX);
63ct_strong_int(domain_id, DOMAIN_ID, size_t, SIZE_MAX);
64ct_strong_int(numa_node, NUMA_NODE, size_t, SIZE_MAX);
65ct_strong_int(thread_id, THREAD_ID, size_t, SIZE_MAX);
66typedef int32_t nice_t;
67typedef uint32_t thread_prio_t;
68
69/* Other types */
70typedef int64_t fx32_32_t;
71typedef ptrdiff_t ssize_t;
72
73/* Larger types */
74typedef __int128_t int128_t;
75typedef __uint128_t uint128_t;
76
77/* "generic" function pointer, our low budget "We have _Any_func at home"
78 * while C2y deliberates. Probably the only place where we typedef
79 * a struct for a piece of the kernel proper, and using an address_space
80 * on Clang */
81struct any_fn_uncallable;
82typedef struct {
83#if defined(cn_id_clang)
84 void cc_mem_any_fn *erased;
85#else
86 void (*erased)(struct any_fn_uncallable);
87#endif
88} any_fn_t;
89ct_assert_same_size(any_fn_t, uintptr_t);
90
91#define any_fn_erase(addr) \
92 ((any_fn_t) {.erased = (typeof(((any_fn_t *) 0)->erased)) (addr)})
93
94/* Constant expression for a function in static init */
95#define any_fn_from(fn) \
96 any_fn_erase( \
97 (uintptr_t) (fn) + \
98 ct_expr_assert_zero(ct_is_fn_ptr(fn), "`" #fn "` is not a function"))
99
100#define any_fn_to(any, T) \
101 (ct_typecheck(any_fn_t, any), \
102 (T) ((uintptr_t) (any).erased + \
103 ct_expr_assert_zero(ct_is_fn_ptr((T) 0), \
104 "`" #T "` is not a function pointer type")))
105
106#define any_fn_eq(a, b) \
107 (ct_typecheck(any_fn_t, a), ct_typecheck(any_fn_t, b), \
108 (a).erased == (b).erased)
109
110#define any_fn_is_null(any) (ct_typecheck(any_fn_t, any), (any).erased == NULL)
111
112/* Sneak it through a void * data slot, which is ok on our targets */
113#define any_fn_to_data(any) \
114 (ct_typecheck(any_fn_t, any), (void *) (uintptr_t) (any).erased)
115
116#define any_fn_from_data(ptr) \
117 (ct_typecheck(void *, ptr), any_fn_erase((uintptr_t) (ptr)))
118
119#define CPU_PERF_MAX INT8_MAX
120#define CPU_PERF_MIN INT8_MIN
121
122#define MODE_MAX UINT16_MAX
123#define MODE_MIN 0
124
125#define GID_MAX UINT32_MAX
126#define GID_MIN 0
127
128#define UID_MAX UINT32_MAX
129#define UID_MIN 0
130
131#define STACK_HANDLE_MAX UINT32_MAX
132#define STACK_HANDLE_MIN 0
133
134#define REFCOUNT_MAX UINT32_MAX
135#define REFCOUNT_MIN 0
136
137#define CPU_ID_NONE CPU_ID_MAX
138
139#define DOMAIN_ID_NONE DOMAIN_ID_MAX
140
141#define THREAD_ID_NONE THREAD_ID_MAX
142
143#define PAGE_FLAGS_MAX UINT64_MAX
144#define PAGE_FLAGS_MIN 0
145
146#define THREAD_PRIO_MAX UINT32_MAX
147#define THREAD_PRIO_MIN 0
148
149#define FX32_32_MAX INT64_MAX
150#define FX32_32_MIN INT64_MIN
151
152#define NICE_MAX 19
153#define NICE_MIN -20
154
155#define SSIZE_MAX ((ssize_t) (SIZE_MAX >> 1))
156#define SSIZE_MIN ((ssize_t) (-SSIZE_MAX - 1))
157
158/* Shift has to come after the complement, because complementing the
159 * shifted zero yields all ones, turning it into `-1` */
160#define INT128_MAX ((int128_t) (~((uint128_t) 0ULL) >> 1))
161#define INT128_MIN ((int128_t) (-INT128_MAX - 1))
162
163#define UINT128_MAX ((uint128_t) (~((uint128_t) 0ULL)))
164#define UINT128_MIN ((uint128_t) 0ULL)
165