xref: /netbsd-src/lib/libc/arch/aarch64/sys/__syscall.S (revision beb9c6d1b5720482272c6602961ee57e797b8c22)
1/* $NetBSD: __syscall.S,v 1.1 2014/08/10 05:47:37 matt Exp $ */
2
3/*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "SYS.h"
33
34#ifndef FUNCNAME
35#define	FUNCNAME	__syscall
36#define SYSTRAP_SYSCALL	SYSTRAP(__syscall)
37#endif
38
39#if SYS_MAXSYSARGS > 8
40#error SYS_MAXSYSARGS is greater than 8
41#endif
42
43ENTRY(FUNCNAME)
44	/*
45	 * AAPCS64 supplies the first 8 arguments in registers but since
46	 * the first argument is the syscall #, we only have room for 7
47	 * arguments.  Thus if the syscall uses all 8 arguments, the last
48	 * argument will have been placed on the stack.  Rather than
49	 * complicating the kernel syscall code to deal with that, we fixup
50	 * things here.
51	 *
52	 * First we move the syscall number to x17 freeing a register.
53	 * Then we move the rest of the arguments to their proper register.
54	 * Finally, load x7 from the stack.
55	 *
56	 * Now everything looks as if the syscall wrapper was directly called.
57	 * (unless it was for pipe, fork, vfork, ptrace, or __clone).
58	 */
59	mov	x17, x0
60	mov	x0, x1
61	mov	x1, x2
62	mov	x2, x3
63	mov	x3, x4
64	mov	x4, x5
65	mov	x5, x6
66	mov	x6, x7
67	ldr	x7, [sp, #0]
68	SYSTRAP_SYSCALL
69	_INVOKE_CERROR()
70	ret
71END(FUNCNAME)
72