1*6777b538SAndroid Build Coastguard Worker# Linux Syscall Support (LSS) 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard WorkerEvery so often, projects need to directly embed Linux system calls instead of 4*6777b538SAndroid Build Coastguard Workercalling the implementations in the system runtime library. 5*6777b538SAndroid Build Coastguard Worker 6*6777b538SAndroid Build Coastguard WorkerThis project provides a header file that can be included into your application 7*6777b538SAndroid Build Coastguard Workerwhenever you need to make direct system calls. 8*6777b538SAndroid Build Coastguard Worker 9*6777b538SAndroid Build Coastguard WorkerThe goal is to provide an API that generally mirrors the standard C library 10*6777b538SAndroid Build Coastguard Workerwhile still making direct syscalls. We try to hide some of the differences 11*6777b538SAndroid Build Coastguard Workerbetween arches when reasonably feasible. e.g. Newer architectures no longer 12*6777b538SAndroid Build Coastguard Workerprovide an `open` syscall, but do provide `openat`. We will still expose a 13*6777b538SAndroid Build Coastguard Worker`sys_open` helper by default that calls into `openat` instead. 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard WorkerWe explicitly do not expose the raw syscall ABI including all of its historical 16*6777b538SAndroid Build Coastguard Workerwarts to the user. We want people to be able to easily make a syscall, not have 17*6777b538SAndroid Build Coastguard Workerto worry that on some arches size args are swapped or they are shifted. 18*6777b538SAndroid Build Coastguard Worker 19*6777b538SAndroid Build Coastguard WorkerPlease be sure to review the Caveats section below however. 20*6777b538SAndroid Build Coastguard Worker 21*6777b538SAndroid Build Coastguard Worker## How to include linux\_syscall\_support.h in your project 22*6777b538SAndroid Build Coastguard Worker 23*6777b538SAndroid Build Coastguard WorkerYou can either copy the file into your project, or preferably, you can set up 24*6777b538SAndroid Build Coastguard WorkerGit submodules to automatically pull from our source repository. 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard Worker## Supported targets 27*6777b538SAndroid Build Coastguard Worker 28*6777b538SAndroid Build Coastguard WorkerThe following architectures/ABIs have been tested (at some point) and should 29*6777b538SAndroid Build Coastguard Workergenerally work. If you don't see your combo listed here, please double check 30*6777b538SAndroid Build Coastguard Workerthe header itself as this list might be out of date. 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker* x86 32-bit (i.e. i386, i486, i586, i686, Intel, AMD, etc...) 33*6777b538SAndroid Build Coastguard Worker* [x86_64 64-bit](https://en.wikipedia.org/wiki/X86-64) (i.e. x86-64, amd64, etc...) 34*6777b538SAndroid Build Coastguard Worker* [x32 32-bit](https://sites.google.com/site/x32abi/) 35*6777b538SAndroid Build Coastguard Worker* [ARM 32-bit](https://en.wikipedia.org/wiki/ARM_architecture) OABI 36*6777b538SAndroid Build Coastguard Worker* [ARM 32-bit](https://en.wikipedia.org/wiki/ARM_architecture) EABI (i.e. armv6, armv7, etc...) 37*6777b538SAndroid Build Coastguard Worker* AARCH64 64-bit (i.e. arm64, armv8, etc...) 38*6777b538SAndroid Build Coastguard Worker* PowerPC 32-bit (i.e. ppc, ppc32, etc...) 39*6777b538SAndroid Build Coastguard Worker* MIPS 32-bit o32 ABI 40*6777b538SAndroid Build Coastguard Worker* MIPS 32-bit n32 ABI 41*6777b538SAndroid Build Coastguard Worker* MIPS 64-bit n64 ABI 42*6777b538SAndroid Build Coastguard Worker* LOONGARCH 64-bit ABI 43*6777b538SAndroid Build Coastguard Worker 44*6777b538SAndroid Build Coastguard Worker## API 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard WorkerBy default, you can just add a `sys_` prefix to any function you want to call. 47*6777b538SAndroid Build Coastguard WorkerSo if you want to call `open(...)`, use `sys_open(...)` instead. 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker### Knobs 50*6777b538SAndroid Build Coastguard Worker 51*6777b538SAndroid Build Coastguard WorkerThe linux\_syscall\_support.h header provides many knobs for you to control 52*6777b538SAndroid Build Coastguard Workerthe exported API. These are all documented in the top of the header in a big 53*6777b538SAndroid Build Coastguard Workercomment block, so refer to that instead. 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker## Caveats 56*6777b538SAndroid Build Coastguard Worker 57*6777b538SAndroid Build Coastguard Worker### ABI differences 58*6777b538SAndroid Build Coastguard Worker 59*6777b538SAndroid Build Coastguard WorkerSome functions that the standard C library exposes use a different ABI than 60*6777b538SAndroid Build Coastguard Workerwhat the Linux kernel uses. Care must be taken when making syscalls directly 61*6777b538SAndroid Build Coastguard Workerthat you use the right structure and flags. e.g. Most C libraries define a 62*6777b538SAndroid Build Coastguard Worker`struct stat` (commonly in `sys/stat.h` or `bits/stat.h`) that is different 63*6777b538SAndroid Build Coastguard Workerfrom the `struct stat` the kernel uses (commonly in `asm/stat.h`). If you use 64*6777b538SAndroid Build Coastguard Workerthe wrong structure layout, then you can see errors like memory corruption or 65*6777b538SAndroid Build Coastguard Workerweird/shifted values. If you plan on making syscalls directly, you should 66*6777b538SAndroid Build Coastguard Workerfocus on headers that are available under the `linux/` and `asm/` namespaces. 67*6777b538SAndroid Build Coastguard Worker 68*6777b538SAndroid Build Coastguard WorkerNote: LSS provides structs for most of these cases. For `sys_stat()`, it 69*6777b538SAndroid Build Coastguard Workerprovides `struct kernel_stat` for you to use. 70*6777b538SAndroid Build Coastguard Worker 71*6777b538SAndroid Build Coastguard Worker### Transparent backwards compatibility with older kernels 72*6777b538SAndroid Build Coastguard Worker 73*6777b538SAndroid Build Coastguard WorkerWhile some C libraries (notably, glibc) take care to fallback to older syscalls 74*6777b538SAndroid Build Coastguard Workerwhen running on older kernels, there is no such support in LSS. If you plan on 75*6777b538SAndroid Build Coastguard Workertrying to run on older kernels, you will need to handle errors yourself (e.g. 76*6777b538SAndroid Build Coastguard Worker`ENOSYS` when using a too new syscall). 77*6777b538SAndroid Build Coastguard Worker 78*6777b538SAndroid Build Coastguard WorkerRemember that this can happen with new flag bits too. e.g. The `O_CLOEXEC` 79*6777b538SAndroid Build Coastguard Workerflag was added to many syscalls, but if you try to run use it on older kernels, 80*6777b538SAndroid Build Coastguard Workerit will fail with `EINVAL`. In that case, you must handle the fallback logic 81*6777b538SAndroid Build Coastguard Workeryourself. 82*6777b538SAndroid Build Coastguard Worker 83*6777b538SAndroid Build Coastguard Worker### Variable arguments (varargs) 84*6777b538SAndroid Build Coastguard Worker 85*6777b538SAndroid Build Coastguard WorkerWe do not support vararg type functions. e.g. While the standard `open()` 86*6777b538SAndroid Build Coastguard Workerfunction can accept 2 or 3 arguments (with the mode field being optional), 87*6777b538SAndroid Build Coastguard Workerthe `sys_open()` function always requires 3 arguments. 88*6777b538SAndroid Build Coastguard Worker 89*6777b538SAndroid Build Coastguard Worker## Bug reports & feature requests 90*6777b538SAndroid Build Coastguard Worker 91*6777b538SAndroid Build Coastguard WorkerIf you wish to report a problem or request a feature, please file them in our 92*6777b538SAndroid Build Coastguard Worker[bug tracker](https://bugs.chromium.org/p/linux-syscall-support/issues/). 93*6777b538SAndroid Build Coastguard Worker 94*6777b538SAndroid Build Coastguard WorkerPlease do not post patches to the tracker. Instead, see below for how to send 95*6777b538SAndroid Build Coastguard Workerpatches to us directly. 96*6777b538SAndroid Build Coastguard Worker 97*6777b538SAndroid Build Coastguard WorkerWhile we welcome feature requests, please keep in mind that it is unlikely that 98*6777b538SAndroid Build Coastguard Workeranyone will find time to implement them for you. Sending patches is strongly 99*6777b538SAndroid Build Coastguard Workerpreferred and will often move things much faster. 100*6777b538SAndroid Build Coastguard Worker 101*6777b538SAndroid Build Coastguard Worker## Projects that use LSS 102*6777b538SAndroid Build Coastguard Worker 103*6777b538SAndroid Build Coastguard Worker* [Chromium](https://www.chromium.org/) 104*6777b538SAndroid Build Coastguard Worker* [Breakpad](https://chromium.googlesource.com/breakpad/breakpad) 105*6777b538SAndroid Build Coastguard Worker* [Native Client](https://developer.chrome.com/native-client), in nacl\_bootstrap.c 106*6777b538SAndroid Build Coastguard Worker 107*6777b538SAndroid Build Coastguard Worker## How to get an LSS change committed 108*6777b538SAndroid Build Coastguard Worker 109*6777b538SAndroid Build Coastguard Worker### Review 110*6777b538SAndroid Build Coastguard Worker 111*6777b538SAndroid Build Coastguard WorkerYou get your change reviewed, you can upload it to 112*6777b538SAndroid Build Coastguard Worker[Gerrit](https://chromium-review.googlesource.com/q/project:linux-syscall-support+status:open) 113*6777b538SAndroid Build Coastguard Workerusing `git cl upload` from 114*6777b538SAndroid Build Coastguard Worker[Chromium's depot-tools](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html). 115*6777b538SAndroid Build Coastguard Worker 116*6777b538SAndroid Build Coastguard Worker### Testing 117*6777b538SAndroid Build Coastguard Worker 118*6777b538SAndroid Build Coastguard WorkerTests are found in the [tests/](./tests/) subdirectory. It does not (yet) offer 119*6777b538SAndroid Build Coastguard Worker100% coverage, but should grow over time. 120*6777b538SAndroid Build Coastguard Worker 121*6777b538SAndroid Build Coastguard WorkerNew commits that update/change/add syscall wrappers should include tests for 122*6777b538SAndroid Build Coastguard Workerthem too. Consult the [test documentation](./tests/README.md) for more details. 123*6777b538SAndroid Build Coastguard Worker 124*6777b538SAndroid Build Coastguard WorkerTo run, just run `make` inside the tests directory. It will compile & execute 125*6777b538SAndroid Build Coastguard Workerthe tests locally. 126*6777b538SAndroid Build Coastguard Worker 127*6777b538SAndroid Build Coastguard WorkerThere is some limited cross-compile coverage available if you run `make cross`. 128*6777b538SAndroid Build Coastguard WorkerIt only compiles things (does not execute at all). 129*6777b538SAndroid Build Coastguard Worker 130*6777b538SAndroid Build Coastguard Worker### Rolling into Chromium 131*6777b538SAndroid Build Coastguard Worker 132*6777b538SAndroid Build Coastguard WorkerIf you commit a change to LSS, please also commit a Chromium change to update 133*6777b538SAndroid Build Coastguard Worker`lss_revision` in 134*6777b538SAndroid Build Coastguard Worker[Chromium's DEPS](https://chromium.googlesource.com/chromium/src/+/HEAD/DEPS) 135*6777b538SAndroid Build Coastguard Workerfile. 136*6777b538SAndroid Build Coastguard Worker 137*6777b538SAndroid Build Coastguard WorkerThis ensures that the LSS change gets tested, so that people who commit later 138*6777b538SAndroid Build Coastguard WorkerLSS changes don't run into problems with updating `lss_revision`. 139