| 1 | /* $Id: kHlpSys-darwin.c 24 2009-02-08 13:58:54Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpBare -
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kStuff.
|
|---|
| 10 | *
|
|---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * In addition to the permissions in the GNU Lesser General Public
|
|---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
|---|
| 18 | * version of this file into combinations with other programs, and to
|
|---|
| 19 | * distribute those combinations without any restriction coming from
|
|---|
| 20 | * the use of this file.
|
|---|
| 21 | *
|
|---|
| 22 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 25 | * Lesser General Public License for more details.
|
|---|
| 26 | *
|
|---|
| 27 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 30 | * 02110-1301, USA
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <k/kHlpSys.h>
|
|---|
| 34 | #include <unistd.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <dlfcn.h>
|
|---|
| 37 | #include <sys/mman.h>
|
|---|
| 38 | #include <mach/mach_time.h>
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | #define USE_DARWIN_SYSCALLS
|
|---|
| 42 |
|
|---|
| 43 | #if K_ARCH == K_ARCH_X86_32
|
|---|
| 44 | # define DARWIN_SYSCALL(name, code) \
|
|---|
| 45 | asm("\
|
|---|
| 46 | .text \n\
|
|---|
| 47 | .globl _" #name " \n\
|
|---|
| 48 | _" #name ": \n\
|
|---|
| 49 | mov $ " #code ", %eax \n\
|
|---|
| 50 | call 1f \n\
|
|---|
| 51 | 1: \n\
|
|---|
| 52 | pop %edx \n\
|
|---|
| 53 | mov %esp, %ecx \n\
|
|---|
| 54 | sysenter \n\
|
|---|
| 55 | jnae 2f \n\
|
|---|
| 56 | ret \n\
|
|---|
| 57 | 2: \n\
|
|---|
| 58 | neg %eax \n\
|
|---|
| 59 | ret \n\
|
|---|
| 60 | ")
|
|---|
| 61 |
|
|---|
| 62 | # define DARWIN_SYSCALL_RET64(name, code) \
|
|---|
| 63 | asm("\
|
|---|
| 64 | .text \n\
|
|---|
| 65 | .globl _" #name " \n\
|
|---|
| 66 | _" #name ": \n\
|
|---|
| 67 | mov $ " #code ", %eax \n\
|
|---|
| 68 | int $0x80 \n\
|
|---|
| 69 | jnae 2f \n\
|
|---|
| 70 | ret \n\
|
|---|
| 71 | 2: \n\
|
|---|
| 72 | neg %eax \n\
|
|---|
| 73 | mov $0xffffffff, %edx \n\
|
|---|
| 74 | ret \n\
|
|---|
| 75 | ")
|
|---|
| 76 |
|
|---|
| 77 | # define DARWIN_SYSCALL_NOERR(name, code) \
|
|---|
| 78 | asm("\
|
|---|
| 79 | .text \n\
|
|---|
| 80 | .globl _" #name " \n\
|
|---|
| 81 | _" #name ": \n\
|
|---|
| 82 | mov $ " #code ", %eax \n\
|
|---|
| 83 | call 1f \n\
|
|---|
| 84 | 1: \n\
|
|---|
| 85 | pop %edx \n\
|
|---|
| 86 | mov %esp, %ecx \n\
|
|---|
| 87 | sysenter \n\
|
|---|
| 88 | ret \n\
|
|---|
| 89 | ")
|
|---|
| 90 |
|
|---|
| 91 | #elif K_ARCH == K_ARCH_AMD64
|
|---|
| 92 | # define DARWIN_SYSCALL(name, code) \
|
|---|
| 93 | asm("\
|
|---|
| 94 | .text \n\
|
|---|
| 95 | .globl _" #name " \n\
|
|---|
| 96 | _" #name ": \n\
|
|---|
| 97 | mov $ " #code ", %eax \n\
|
|---|
| 98 | mov %rcx, %r10 \n\
|
|---|
| 99 | sysenter \n\
|
|---|
| 100 | jnae 2f \n\
|
|---|
| 101 | ret \n\
|
|---|
| 102 | 2: \n\
|
|---|
| 103 | neg %eax \n\
|
|---|
| 104 | movsx %eax, %rax \n\
|
|---|
| 105 | ret \n\
|
|---|
| 106 | ")
|
|---|
| 107 |
|
|---|
| 108 | # define DARWIN_SYSCALL_RET64(name, code) DARWIN_SYSCALL_RET(name, code)
|
|---|
| 109 |
|
|---|
| 110 | # define DARWIN_SYSCALL_NOERR(name, code) \
|
|---|
| 111 | asm("\
|
|---|
| 112 | .text \n\
|
|---|
| 113 | .globl _" #name " \n\
|
|---|
| 114 | _" #name ": \n\
|
|---|
| 115 | mov $ " #code ", %eax \n\
|
|---|
| 116 | mov %rcx, %r10 \n\
|
|---|
| 117 | sysenter \n\
|
|---|
| 118 | ret \n\
|
|---|
| 119 | ")
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | #else
|
|---|
| 123 | # error later...
|
|---|
| 124 | #endif
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 128 | DARWIN_SYSCALL(kHlpSys_readlink, 0x000c003a);
|
|---|
| 129 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 130 | DARWIN_SYSCALL(kHlpSys_readlink, 0x0200003a);
|
|---|
| 131 | #else
|
|---|
| 132 | KSSIZE kHlpSys_readlink(const char *pszPath, char *pszBuf, KSIZE cbBuf)
|
|---|
| 133 | {
|
|---|
| 134 | KSSIZE cbRet = readlink(pszPath, pszBuf, cbBuf);
|
|---|
| 135 | return cbRet >= 0 ? cbRet : -errno;
|
|---|
| 136 | }
|
|---|
| 137 | #endif
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 141 | DARWIN_SYSCALL(kHlpSys_open, 0x000c0005);
|
|---|
| 142 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 143 | DARWIN_SYSCALL(kHlpSys_open, 0x02000005);
|
|---|
| 144 | #else
|
|---|
| 145 | int kHlpSys_open(const char *filename, int flags, int mode)
|
|---|
| 146 | {
|
|---|
| 147 | int fd = open(filename, flags, mode);
|
|---|
| 148 | return fd >= 0 ? fd : -errno;
|
|---|
| 149 | }
|
|---|
| 150 | #endif
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 154 | DARWIN_SYSCALL(kHlpSys_close, 0x000c0006);
|
|---|
| 155 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 156 | DARWIN_SYSCALL(kHlpSys_close, 0x02000006);
|
|---|
| 157 | #else
|
|---|
| 158 | int kHlpSys_close(int fd)
|
|---|
| 159 | {
|
|---|
| 160 | if (!close(fd))
|
|---|
| 161 | return 0;
|
|---|
| 162 | return -errno;
|
|---|
| 163 | }
|
|---|
| 164 | #endif
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 168 | DARWIN_SYSCALL_RET64(kHlpSys_lseek, 0x000000c7);
|
|---|
| 169 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 170 | DARWIN_SYSCALL_RET64(kHlpSys_lseek, 0x020000c7);
|
|---|
| 171 | #else
|
|---|
| 172 | KFOFF kHlpSys_lseek(int fd, int whench, KFOFF off)
|
|---|
| 173 | {
|
|---|
| 174 | KFOFF offRet = lseek(fd, whench, off);
|
|---|
| 175 | return offRet >= 0 ? offRet : -errno;
|
|---|
| 176 | }
|
|---|
| 177 | #endif
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 181 | DARWIN_SYSCALL(kHlpSys_read, 0x000c0003);
|
|---|
| 182 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 183 | DARWIN_SYSCALL(kHlpSys_read, 0x02000003);
|
|---|
| 184 | #else
|
|---|
| 185 | KSSIZE kHlpSys_read(int fd, void *pvBuf, KSIZE cbBuf)
|
|---|
| 186 | {
|
|---|
| 187 | KSSIZE cbRead = read(fd, pvBuf, cbBuf);
|
|---|
| 188 | return cbRead >= 0 ? cbRead : -errno;
|
|---|
| 189 | }
|
|---|
| 190 | #endif
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 194 | DARWIN_SYSCALL(kHlpSys_write, 0x000c0004);
|
|---|
| 195 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 196 | DARWIN_SYSCALL(kHlpSys_write, 0x02000004);
|
|---|
| 197 | #else
|
|---|
| 198 | KSSIZE kHlpSys_write(int fd, const void *pvBuf, KSIZE cbBuf)
|
|---|
| 199 | {
|
|---|
| 200 | KSSIZE cbWritten = write(fd, pvBuf, cbBuf);
|
|---|
| 201 | return cbWritten >= 0 ? cbWritten : -errno;
|
|---|
| 202 | }
|
|---|
| 203 | #endif
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 207 | DARWIN_SYSCALL(kHlpSys_mmap, 0x020000c5);
|
|---|
| 208 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 209 | DARWIN_SYSCALL(kHlpSys_mmap, 0x020000c5);
|
|---|
| 210 | #else
|
|---|
| 211 | void *kHlpSys_mmap(void *addr, KSIZE len, int prot, int flags, int fd, KI64 off)
|
|---|
| 212 | {
|
|---|
| 213 | void *pv = mmap(addr, len, prot, flags, fd, off);
|
|---|
| 214 | return pv != (void *)-1
|
|---|
| 215 | ? pv
|
|---|
| 216 | : errno < 256 ? (void *)(long)errno : (void *)(long)ENOMEM;
|
|---|
| 217 | }
|
|---|
| 218 | #endif
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 222 | DARWIN_SYSCALL(kHlpSys_mprotect, 0x000c004a);
|
|---|
| 223 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 224 | DARWIN_SYSCALL(kHlpSys_mprotect, 0x0200004a);
|
|---|
| 225 | #else
|
|---|
| 226 | int kHlpSys_mprotect(void *addr, KSIZE len, int prot)
|
|---|
| 227 | {
|
|---|
| 228 | if (!mprotect(addr, len, prot))
|
|---|
| 229 | return 0;
|
|---|
| 230 | return -errno;
|
|---|
| 231 | }
|
|---|
| 232 | #endif
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 236 | DARWIN_SYSCALL(kHlpSys_munmap, 0x00080049);
|
|---|
| 237 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 238 | DARWIN_SYSCALL(kHlpSys_munmap, 0x02000049);
|
|---|
| 239 | #else
|
|---|
| 240 | int kHlpSys_munmap(void *addr, KSIZE len)
|
|---|
| 241 | {
|
|---|
| 242 | if (!munmap(addr, len))
|
|---|
| 243 | return 0;
|
|---|
| 244 | return -errno;
|
|---|
| 245 | }
|
|---|
| 246 | #endif
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 250 | DARWIN_SYSCALL(kHlpSys_exit, 0x00040001);
|
|---|
| 251 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 252 | DARWIN_SYSCALL(kHlpSys_exit, 0x02000001);
|
|---|
| 253 | #else
|
|---|
| 254 | void kHlpSys_exit(int rc)
|
|---|
| 255 | {
|
|---|
| 256 | _Exit(rc);
|
|---|
| 257 | }
|
|---|
| 258 | #endif
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | /*
|
|---|
| 262 | * Some other stuff we'll be needing - Move to an appropriate place?
|
|---|
| 263 | */
|
|---|
| 264 |
|
|---|
| 265 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 266 | DARWIN_SYSCALL_NOERR(mach_task_self, 0xffffffe4);
|
|---|
| 267 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 268 | DARWIN_SYSCALL_NOERR(mach_task_self, 0xffffffe4);
|
|---|
| 269 | #endif
|
|---|
| 270 |
|
|---|
| 271 | //#if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 272 | //DARWIN_SYSCALL(semaphore_create, 0x00040001);
|
|---|
| 273 | //#elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 274 | //DARWIN_SYSCALL(semaphore_create, 0x02000001);
|
|---|
| 275 | //#endif
|
|---|
| 276 | #ifdef USE_DARWIN_SYSCALLS
|
|---|
| 277 | kern_return_t semaphore_create(task_t t, semaphore_t *ps, int p, int v)
|
|---|
| 278 | {
|
|---|
| 279 | return 0;
|
|---|
| 280 | }
|
|---|
| 281 | #endif
|
|---|
| 282 |
|
|---|
| 283 | //#if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 284 | //DARWIN_SYSCALL(semaphore_destroy, 0x00040001);
|
|---|
| 285 | //#elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 286 | //DARWIN_SYSCALL(semaphore_destroy, 0x02000001);
|
|---|
| 287 | //#endif
|
|---|
| 288 | #ifdef USE_DARWIN_SYSCALLS
|
|---|
| 289 | kern_return_t semaphore_destroy(task_t t, semaphore_t s)
|
|---|
| 290 | {
|
|---|
| 291 | return 0;
|
|---|
| 292 | }
|
|---|
| 293 | #endif
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 297 | DARWIN_SYSCALL(semaphore_wait, 0xffffffdc);
|
|---|
| 298 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 299 | DARWIN_SYSCALL(semaphore_wait, 0xffffffdc);
|
|---|
| 300 | #endif
|
|---|
| 301 |
|
|---|
| 302 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 303 | DARWIN_SYSCALL(semaphore_signal, 0xffffffdf);
|
|---|
| 304 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 305 | DARWIN_SYSCALL(semaphore_signal, 0xffffffdf);
|
|---|
| 306 | #endif
|
|---|
| 307 |
|
|---|
| 308 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 309 | DARWIN_SYSCALL(mach_wait_until, 0xffffffa6);
|
|---|
| 310 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 311 | DARWIN_SYSCALL(mach_wait_until, 0xffffffa6);
|
|---|
| 312 | #endif
|
|---|
| 313 |
|
|---|
| 314 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 315 | DARWIN_SYSCALL(mach_timebase_info, 0xffffffa7);
|
|---|
| 316 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 317 | DARWIN_SYSCALL(mach_timebase_info, 0xffffffa7);
|
|---|
| 318 | #endif
|
|---|
| 319 |
|
|---|
| 320 | #if K_ARCH == K_ARCH_X86_32 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 321 | asm("\n\
|
|---|
| 322 | .text \n\
|
|---|
| 323 | .globl _mach_absolute_time \n\
|
|---|
| 324 | _mach_absolute_time: \n\
|
|---|
| 325 | mov $0xffff1700, %edx \n\
|
|---|
| 326 | jmp *%edx\n"); /* common page stuff. */
|
|---|
| 327 | #elif K_ARCH == K_ARCH_AMD64 && defined(USE_DARWIN_SYSCALLS)
|
|---|
| 328 | #endif
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 | void *dlopen(const char *pszModule, int fFlags)
|
|---|
| 332 | {
|
|---|
| 333 | return NULL;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 | int dlclose(void *pvMod)
|
|---|
| 338 | {
|
|---|
| 339 |
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 | void *dlsym(void *pvMod, const char *pszSymbol)
|
|---|
| 344 | {
|
|---|
| 345 | return NULL;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|