]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/cloudabi64/cloudabi64_sysvec.c
Use the new insecure-lan-zones option instead of listing each AS112 zone
[FreeBSD/FreeBSD.git] / sys / amd64 / cloudabi64 / cloudabi64_sysvec.c
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/proc.h>
32 #include <sys/sysent.h>
33
34 #include <vm/vm.h>
35 #include <vm/pmap.h>
36
37 #include <machine/frame.h>
38 #include <machine/pcb.h>
39 #include <machine/pmap.h>
40 #include <machine/vmparam.h>
41
42 #include <compat/cloudabi/cloudabi_util.h>
43
44 #include <compat/cloudabi64/cloudabi64_syscall.h>
45 #include <compat/cloudabi64/cloudabi64_util.h>
46
47 extern const char *cloudabi64_syscallnames[];
48 extern struct sysent cloudabi64_sysent[];
49
50 static int
51 cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
52 {
53         struct trapframe *frame = td->td_frame;
54
55         /* Obtain system call number. */
56         sa->code = frame->tf_rax;
57         if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
58                 return (ENOSYS);
59         sa->callp = &cloudabi64_sysent[sa->code];
60
61         /* Fetch system call arguments. */
62         sa->args[0] = frame->tf_rdi;
63         sa->args[1] = frame->tf_rsi;
64         sa->args[2] = frame->tf_rdx;
65         sa->args[3] = frame->tf_rcx; /* Actually %r10. */
66         sa->args[4] = frame->tf_r8;
67         sa->args[5] = frame->tf_r9;
68
69         /* Default system call return values. */
70         td->td_retval[0] = 0;
71         td->td_retval[1] = frame->tf_rdx;
72         return (0);
73 }
74
75 static void
76 cloudabi64_set_syscall_retval(struct thread *td, int error)
77 {
78         struct trapframe *frame = td->td_frame;
79
80         switch (error) {
81         case 0:
82                 /* System call succeeded. */
83                 frame->tf_rax = td->td_retval[0];
84                 frame->tf_rdx = td->td_retval[1];
85                 frame->tf_rflags &= ~PSL_C;
86                 break;
87         case ERESTART:
88                 /* Restart system call. */
89                 frame->tf_rip -= frame->tf_err;
90                 frame->tf_r10 = frame->tf_rcx;
91                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
92                 break;
93         case EJUSTRETURN:
94                 break;
95         default:
96                 /* System call returned an error. */
97                 frame->tf_rax = cloudabi_convert_errno(error);
98                 frame->tf_rflags |= PSL_C;
99                 break;
100         }
101 }
102
103 static void
104 cloudabi64_schedtail(struct thread *td)
105 {
106         struct trapframe *frame = td->td_frame;
107
108         /* Initial register values for processes returning from fork. */
109         frame->tf_rax = CLOUDABI_PROCESS_CHILD;
110         frame->tf_rdx = td->td_tid;
111 }
112
113 void
114 cloudabi64_thread_setregs(struct thread *td,
115     const cloudabi64_threadattr_t *attr)
116 {
117         struct trapframe *frame;
118         stack_t stack;
119
120         /* Perform standard register initialization. */
121         stack.ss_sp = (void *)attr->stack;
122         stack.ss_size = attr->stack_size;
123         cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
124
125         /*
126          * Pass in the thread ID of the new thread and the argument
127          * pointer provided by the parent thread in as arguments to the
128          * entry point.
129          */
130         frame = td->td_frame;
131         frame->tf_rdi = td->td_tid;
132         frame->tf_rsi = attr->argument;
133 }
134
135 static struct sysentvec cloudabi64_elf_sysvec = {
136         .sv_size                = CLOUDABI64_SYS_MAXSYSCALL,
137         .sv_table               = cloudabi64_sysent,
138         .sv_fixup               = cloudabi64_fixup,
139         .sv_name                = "CloudABI ELF64",
140         .sv_coredump            = elf64_coredump,
141         .sv_pagesize            = PAGE_SIZE,
142         .sv_minuser             = VM_MIN_ADDRESS,
143         .sv_maxuser             = VM_MAXUSER_ADDRESS,
144         .sv_usrstack            = USRSTACK,
145         .sv_stackprot           = VM_PROT_READ | VM_PROT_WRITE,
146         .sv_copyout_strings     = cloudabi64_copyout_strings,
147         .sv_flags               = SV_ABI_CLOUDABI | SV_CAPSICUM,
148         .sv_set_syscall_retval  = cloudabi64_set_syscall_retval,
149         .sv_fetch_syscall_args  = cloudabi64_fetch_syscall_args,
150         .sv_syscallnames        = cloudabi64_syscallnames,
151         .sv_schedtail           = cloudabi64_schedtail,
152 };
153
154 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
155
156 Elf64_Brandinfo cloudabi64_brand = {
157         .brand          = ELFOSABI_CLOUDABI,
158         .machine        = EM_X86_64,
159         .sysvec         = &cloudabi64_elf_sysvec,
160         .compat_3_brand = "CloudABI",
161 };