Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/svc/ck_compat.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / svc / ck_compat.c` 128 lines ยท 4145 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* ck_compat.c -- kernel-build implementations behind the svc/compat headers and the 18 * four memory routines GCC may emit calls to. Freestanding; ck.h only. 19 * Not linked into the host tests (they use libc). */ 20 #include 21 #include 22 #include "ck.h" 23 #include "ck_compat.h" 24 #include "compat/pthread.h" 25 #include "compat/stdio.h" 26 #include "compat/time.h" 27 28 /* ---- memory routines: weak, so a core copy wins at link time ---------- */ 29 __attribute__((weak)) void *memcpy(void *restrict d, const void *restrict s, size_t n) 30 { 31 unsigned char *a = d; 32 const unsigned char *b = s; 33 while (n--) *a++ = *b++; 34 return d; 35 } 36 __attribute__((weak)) void *memmove(void *d, const void *s, size_t n) 37 { 38 unsigned char *a = d; 39 const unsigned char *b = s; 40 if (a < b) { 41 while (n--) *a++ = *b++; 42 } else { 43 while (n--) a[n] = b[n]; 44 } 45 return d; 46 } 47 __attribute__((weak)) void *memset(void *d, int c, size_t n) 48 { 49 unsigned char *a = d; 50 while (n--) *a++ = (unsigned char)c; 51 return d; 52 } 53 __attribute__((weak)) int memcmp(const void *x, const void *y, size_t n) 54 { 55 const unsigned char *a = x, *b = y; 56 for (; n; n--, a++, b++) 57 if (*a != *b) return *a < *b ? -1 : 1; 58 return 0; 59 } 60 61 /* ---- heap, clock, lock ------------------------------------------------- */ 62 void *ck_compat_malloc(size_t n) { return ck_alloc(n ? n : 1); } 63 void *ck_compat_calloc(size_t n, size_t sz) 64 { 65 if (sz && n > (size_t)-1 / sz) return 0; 66 size_t total = n * sz; 67 return ck_alloc(total ? total : 1); /* ck_alloc memory is zeroed (ck.h) */ 68 } 69 void ck_compat_free(void *p) 70 { 71 if (p) ck_free(p); 72 } 73 int ck_compat_clock_gettime(clockid_t id, struct timespec *ts) 74 { 75 (void)id; 76 if (!ts) return -1; 77 uint64_t us = ck_time_us(); 78 ts->tv_sec = (time_t)(us / 1000000u); 79 ts->tv_nsec = (long)((us % 1000000u) * 1000u); 80 return 0; 81 } 82 void ck_compat_mutex_lock(pthread_mutex_t *m) 83 { 84 if (m->locked) ck_panic("ck_compat: mutex re-entered (single-core stage code)"); 85 m->locked = 1; 86 } 87 88 /* ---- entropy for "/dev/urandom" --------------------------------------- */ 89 /* The only kernel consumer is the capability office token 90 * (native/capability fill_token). Bytes come from ck_entropy_fill (RNDR, 91 * bounded retry, stuck test) or the read is refused: fread returns 0, the 92 * capability library fails closed (aienos_cap_start refuses) and the 93 * security stage (capability office + ARGUS) reports FAIL. There is no 94 * fallback source of any kind. */ 95 static int g_entropy = CK_ENTROPY_NONE; 96 int ck_compat_entropy_source(void) { return g_entropy; } 97 98 struct ck_compat_file { int open; }; 99 static struct ck_compat_file urandom_file; 100 101 FILE *ck_compat_fopen(const char *path, const char *mode) 102 { 103 (void)mode; 104 const char *want = "/dev/urandom"; 105 for (int i = 0;; i++) { 106 if (path[i] != want[i]) return 0; 107 if (!want[i]) break; 108 } 109 urandom_file.open = 1; 110 return &urandom_file; 111 } 112 size_t ck_compat_fread(void *buf, size_t sz, size_t n, FILE *f) 113 { 114 if (!f || !f->open || sz == 0 || n == 0 || n > (size_t)-1 / sz) return 0; 115 if (ck_entropy_fill(buf, sz * n) != 0) { 116 g_entropy = CK_ENTROPY_NONE; 117 ck_printf("entropy: refused consumer=/dev/urandom (capability office token) reason=%s; " 118 "service fails closed\n", ck_entropy_reason()); 119 return 0; 120 } 121 g_entropy = CK_ENTROPY_RNDR; 122 return n; 123 } 124 int ck_compat_fclose(FILE *f) 125 { 126 if (f) f->open = 0; 127 return 0; 128 }