Skip to content

Commit

Permalink
Automatically generate system call prototypes for the Linux kernel.
Browse files Browse the repository at this point in the history
Inside of the Linux kernel we have the possibility of simply using the
same prototypes as what we use for userspace, as long as our system call
table has wrappers to invoke them in the right way. Already check in the
generator for the header files for our prototypes. Some differences
compared to userspace:

- We don't want them to be static inline. Move this into the system call
  implementation generator.
- Don't use _Noreturn. The implementations of these functions need to
  invoke other functions that are not marked that way.
- Add __user annotations to pointers going to userspace.

I'm now working on patching up all of the system calls to use these
prototypes. When this is completed, I'll work on automatically
generating a system call table.
  • Loading branch information
EdSchouten committed Mar 25, 2016
1 parent 818c03c commit c015f43
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 9 deletions.
19 changes: 19 additions & 0 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@
naming=CNaming('cloudabi_', 'cloudabi64_', c11=False),
).generate_abi(abi)

with open('linux/cloudabi_syscalls.h', 'w') as f:
with redirect_stdout(f):
CSyscallsGenerator(
naming=CNaming('cloudabi_', c11=False, pointer_prefix='__user '),
header_guard='CLOUDABI_SYSCALLS_H',
machine_dep=False,
preamble='#include "cloudabi_types_common.h"\n'
).generate_abi(abi)

with open('linux/cloudabi64_syscalls.h', 'w') as f:
with redirect_stdout(f):
CSyscallsGenerator(
naming=CNaming('cloudabi_', 'cloudabi64_', c11=False,
pointer_prefix='__user '),
header_guard='CLOUDABI64_SYSCALLS_H',
machine_dep=True,
preamble='#include "cloudabi64_types.h"\n'
).generate_abi(abi)

with open('docs/cloudabi.md', 'w') as f:
with redirect_stdout(f):
MarkdownGenerator(
Expand Down
4 changes: 4 additions & 0 deletions generator/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def __init__(self, target_type=VoidType(), const=False):
self.target_type = target_type


class OutputPointerType(PointerType):
pass


class AtomicType(Type):

def __init__(self, target_type):
Expand Down
36 changes: 27 additions & 9 deletions generator/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

class CNaming:

def __init__(self, prefix, md_prefix=None, c11=True):
def __init__(self, prefix, md_prefix=None, c11=True, pointer_prefix=''):
self.prefix = prefix
self.md_prefix = md_prefix
self.c11 = c11
self.pointer_prefix = pointer_prefix

def typename(self, type):
if isinstance(type, VoidType):
Expand Down Expand Up @@ -48,8 +49,12 @@ def syscallname(self, syscall):
return '{}sys_{}'.format(prefix, syscall.name)

def vardecl(self, type, name, array_need_parens=False):
if isinstance(type, PointerType):
decl = self.vardecl(type.target_type, '*{}'.format(name),
if isinstance(type, OutputPointerType):
return self.vardecl(type.target_type, '*{}'.format(name),
array_need_parens=True)
elif isinstance(type, PointerType):
decl = self.vardecl(type.target_type,
'{}*{}'.format(self.pointer_prefix, name),
array_need_parens=True)
if type.const:
decl = 'const ' + decl
Expand Down Expand Up @@ -240,18 +245,21 @@ def syscall_params(self, syscall):
for p in syscall.input.raw_members:
params.append(self.naming.vardecl(p.type, p.name))
for p in syscall.output.raw_members:
params.append(self.naming.vardecl(PointerType(p.type), p.name))
params.append(self.naming.vardecl(OutputPointerType(p.type),
p.name))
return params

def generate_syscall(self, abi, syscall):
if self.machine_dep is not None:
if syscall.machine_dep != self.machine_dep:
return

self.generate_syscall_keywords(syscall)
if syscall.noreturn:
noreturn = '_Noreturn '
return_type = VoidType()
else:
noreturn = ''
return_type = UserDefinedType('errno')
print('static inline {}{}'.format(
noreturn, self.naming.typename(return_type)))
return_type = IntLikeType('errno', IntType('uint8', 1), set(), False)
print(self.naming.typename(return_type))
print('{}('.format(self.naming.syscallname(syscall)), end='')
params = self.syscall_params(syscall)
if params == []:
Expand All @@ -265,6 +273,9 @@ def generate_syscall(self, abi, syscall):
self.generate_syscall_body(abi, syscall)
print()

def generate_syscall_keywords(self, syscall):
pass

def generate_syscall_body(self, abi, syscall):
print(';')

Expand All @@ -274,6 +285,13 @@ def generate_types(self, abi, types):

class CSyscallsImplGenerator(CSyscallsGenerator):

def generate_syscall_keywords(self, syscall):
if syscall.noreturn:
noreturn = '_Noreturn '
else:
noreturn = ''
print('static inline {}'.format(noreturn), end='')

def generate_syscall_body(self, abi, syscall):
print(' {')

Expand Down
106 changes: 106 additions & 0 deletions linux/cloudabi64_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2016 Nuxi (https://nuxi.nl/) and contributors.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// This file is automatically generated. Do not edit.
//
// Source: https://github.com/NuxiNL/cloudabi

#ifndef CLOUDABI64_SYSCALLS_H
#define CLOUDABI64_SYSCALLS_H

#include "cloudabi64_types.h"

cloudabi_errno_t
cloudabi64_sys_fd_pread(
cloudabi_fd_t fd,
const cloudabi64_iovec_t __user *iov,
size_t iovcnt,
cloudabi_filesize_t offset,
size_t *nread
);

cloudabi_errno_t
cloudabi64_sys_fd_pwrite(
cloudabi_fd_t fd,
const cloudabi64_ciovec_t __user *iov,
size_t iovcnt,
cloudabi_filesize_t offset,
size_t *nwritten
);

cloudabi_errno_t
cloudabi64_sys_fd_read(
cloudabi_fd_t fd,
const cloudabi64_iovec_t __user *iov,
size_t iovcnt,
size_t *nread
);

cloudabi_errno_t
cloudabi64_sys_fd_write(
cloudabi_fd_t fd,
const cloudabi64_ciovec_t __user *iov,
size_t iovcnt,
size_t *nwritten
);

cloudabi_errno_t
cloudabi64_sys_poll(
const cloudabi64_subscription_t __user *in,
cloudabi64_event_t __user *out,
size_t nsubscriptions,
size_t *nevents
);

cloudabi_errno_t
cloudabi64_sys_poll_fd(
cloudabi_fd_t fd,
const cloudabi64_subscription_t __user *in,
size_t nin,
cloudabi64_event_t __user *out,
size_t nout,
const cloudabi64_subscription_t __user *timeout,
size_t *nevents
);

cloudabi_errno_t
cloudabi64_sys_sock_recv(
cloudabi_fd_t sock,
const cloudabi64_recv_in_t __user *in,
cloudabi64_recv_out_t __user *out
);

cloudabi_errno_t
cloudabi64_sys_sock_send(
cloudabi_fd_t sock,
const cloudabi64_send_in_t __user *in,
cloudabi64_send_out_t __user *out
);

cloudabi_errno_t
cloudabi64_sys_thread_create(
cloudabi64_threadattr_t __user *attr,
cloudabi_tid_t *tid
);

#endif
Loading

0 comments on commit c015f43

Please sign in to comment.