Rev 965 | Rev 1005 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
662 | decky | 1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
678 | decky | 29 | #include <syscall/syscall.h> |
714 | decky | 30 | #include <proc/thread.h> |
977 | jermar | 31 | #include <mm/as.h> |
712 | decky | 32 | #include <print.h> |
33 | #include <putchar.h> |
||
955 | palkovsky | 34 | #include <ipc/ipc.h> |
35 | #include <errno.h> |
||
36 | #include <proc/task.h> |
||
37 | #include <arch.h> |
||
38 | #include <debug.h> |
||
662 | decky | 39 | |
955 | palkovsky | 40 | static __native sys_ctl(void) { |
714 | decky | 41 | printf("Thread finished\n"); |
42 | thread_exit(); |
||
43 | /* Unreachable */ |
||
662 | decky | 44 | return 0; |
45 | } |
||
46 | |||
955 | palkovsky | 47 | static __native sys_io(int fd, const void * buf, size_t count) { |
712 | decky | 48 | |
49 | // TODO: buf sanity checks and a lot of other stuff ... |
||
50 | |||
51 | size_t i; |
||
52 | |||
53 | for (i = 0; i < count; i++) |
||
54 | putchar(((char *) buf)[i]); |
||
55 | |||
714 | decky | 56 | return count; |
662 | decky | 57 | } |
58 | |||
959 | palkovsky | 59 | /** Send a call over IPC, wait for reply, return to user |
955 | palkovsky | 60 | * |
61 | * @return Call identification, returns -1 on fatal error, |
||
62 | -2 on 'Too many async request, handle answers first |
||
63 | */ |
||
965 | palkovsky | 64 | static __native sys_ipc_call_sync(__native phoneid, __native method, |
65 | __native arg1, __native *data) |
||
955 | palkovsky | 66 | { |
965 | palkovsky | 67 | call_t call; |
955 | palkovsky | 68 | phone_t *phone; |
959 | palkovsky | 69 | /* Special answerbox for synchronous messages */ |
955 | palkovsky | 70 | |
71 | if (phoneid >= IPC_MAX_PHONES) |
||
965 | palkovsky | 72 | return IPC_CALLRET_FATAL; |
955 | palkovsky | 73 | |
74 | phone = &TASK->phones[phoneid]; |
||
75 | if (!phone->callee) |
||
965 | palkovsky | 76 | return IPC_CALLRET_FATAL; |
955 | palkovsky | 77 | |
965 | palkovsky | 78 | ipc_call_init(&call); |
79 | IPC_SET_METHOD(call.data, method); |
||
80 | IPC_SET_ARG1(call.data, arg1); |
||
959 | palkovsky | 81 | |
965 | palkovsky | 82 | ipc_call_sync(phone, &call); |
955 | palkovsky | 83 | |
965 | palkovsky | 84 | copy_to_uspace(data, &call.data, sizeof(call.data)); |
959 | palkovsky | 85 | |
86 | return 0; |
||
87 | } |
||
88 | |||
965 | palkovsky | 89 | static __native sys_ipc_call_sync_medium(__native phoneid, __native *data) |
90 | { |
||
91 | call_t call; |
||
92 | phone_t *phone; |
||
93 | /* Special answerbox for synchronous messages */ |
||
94 | |||
95 | if (phoneid >= IPC_MAX_PHONES) |
||
96 | return IPC_CALLRET_FATAL; |
||
97 | |||
98 | phone = &TASK->phones[phoneid]; |
||
99 | if (!phone->callee) |
||
100 | return IPC_CALLRET_FATAL; |
||
101 | |||
102 | ipc_call_init(&call); |
||
103 | copy_from_uspace(&call.data, data, sizeof(call.data)); |
||
104 | |||
105 | ipc_call_sync(phone, &call); |
||
106 | |||
107 | copy_to_uspace(data, &call.data, sizeof(call.data)); |
||
108 | |||
109 | return 0; |
||
110 | } |
||
111 | |||
112 | |||
959 | palkovsky | 113 | /** Send an asynchronous call over ipc |
114 | * |
||
115 | * @return Call identification, returns -1 on fatal error, |
||
116 | -2 on 'Too many async request, handle answers first |
||
117 | */ |
||
965 | palkovsky | 118 | static __native sys_ipc_call_async(__native phoneid, __native method, |
119 | __native arg1, __native arg2) |
||
959 | palkovsky | 120 | { |
121 | call_t *call; |
||
122 | phone_t *phone; |
||
123 | |||
124 | if (phoneid >= IPC_MAX_PHONES) |
||
965 | palkovsky | 125 | return IPC_CALLRET_FATAL; |
959 | palkovsky | 126 | |
127 | phone = &TASK->phones[phoneid]; |
||
128 | if (!phone->callee) |
||
965 | palkovsky | 129 | return IPC_CALLRET_FATAL; |
959 | palkovsky | 130 | |
955 | palkovsky | 131 | /* TODO: Check that we did not exceed system imposed maximum |
132 | * of asynchrnously sent messages |
||
133 | * - the userspace should be able to handle it correctly |
||
134 | */ |
||
135 | call = ipc_call_alloc(); |
||
965 | palkovsky | 136 | IPC_SET_METHOD(call->data, method); |
137 | IPC_SET_ARG1(call->data, arg1); |
||
138 | IPC_SET_ARG2(call->data, arg2); |
||
139 | |||
955 | palkovsky | 140 | ipc_call(phone, call); |
141 | |||
142 | return (__native) call; |
||
143 | } |
||
144 | |||
145 | /** Send IPC answer */ |
||
965 | palkovsky | 146 | static __native sys_ipc_answer(__native callid, __native retval, __native arg1, |
147 | __native arg2) |
||
955 | palkovsky | 148 | { |
149 | call_t *call; |
||
150 | |||
151 | /* Check that the user is not sending us answer callid */ |
||
152 | ASSERT(! (callid & 1)); |
||
153 | /* TODO: Check that the callid is in the dispatch table */ |
||
154 | call = (call_t *) callid; |
||
155 | |||
965 | palkovsky | 156 | IPC_SET_RETVAL(call->data, retval); |
157 | IPC_SET_ARG1(call->data, arg1); |
||
158 | IPC_SET_ARG2(call->data, arg2); |
||
955 | palkovsky | 159 | |
160 | ipc_answer(&TASK->answerbox, call); |
||
161 | return 0; |
||
162 | } |
||
163 | |||
164 | /** Wait for incoming ipc call or answer |
||
165 | * |
||
166 | * @param result |
||
167 | * @param flags |
||
168 | * @return Callid, if callid & 1, then the call is answer |
||
169 | */ |
||
170 | static __native sys_ipc_wait_for_call(__native *calldata, __native flags) |
||
171 | { |
||
172 | call_t *call; |
||
173 | |||
174 | call = ipc_wait_for_call(&TASK->answerbox, flags); |
||
175 | |||
965 | palkovsky | 176 | copy_to_uspace(calldata, &call->data, sizeof(call->data)); |
177 | if (call->flags & IPC_CALL_ANSWERED) { |
||
178 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
||
179 | ipc_call_free(call); |
||
180 | return ((__native)call) | IPC_CALLID_ANSWERED; |
||
181 | } |
||
955 | palkovsky | 182 | return (__native)call; |
183 | } |
||
184 | |||
977 | jermar | 185 | static __native sys_mremap(void *address, size_t size, unsigned long flags) |
186 | { |
||
187 | return as_remap(AS, (__address) address, size, 0); |
||
188 | } |
||
955 | palkovsky | 189 | |
662 | decky | 190 | syshandler_t syscall_table[SYSCALL_END] = { |
191 | sys_ctl, |
||
955 | palkovsky | 192 | sys_io, |
977 | jermar | 193 | sys_mremap, |
959 | palkovsky | 194 | sys_ipc_call_sync, |
965 | palkovsky | 195 | sys_ipc_call_sync_medium, |
959 | palkovsky | 196 | sys_ipc_call_async, |
955 | palkovsky | 197 | sys_ipc_answer, |
198 | sys_ipc_wait_for_call |
||
662 | decky | 199 | }; |