Rev 2479 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2479 | Rev 2518 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Michal Konopa |
2 | * Copyright (c) 2007 Michal Konopa |
3 | * Copyright (c) 2007 Martin Jelen |
3 | * Copyright (c) 2007 Martin Jelen |
4 | * Copyright (c) 2007 Peter Majer |
4 | * Copyright (c) 2007 Peter Majer |
5 | * Copyright (c) 2007 Jakub Jermar |
5 | * Copyright (c) 2007 Jakub Jermar |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
10 | * are met: |
11 | * |
11 | * |
12 | * - Redistributions of source code must retain the above copyright |
12 | * - Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * - Redistributions in binary form must reproduce the above copyright |
14 | * - Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
16 | * documentation and/or other materials provided with the distribution. |
17 | * - The name of the author may not be used to endorse or promote products |
17 | * - The name of the author may not be used to endorse or promote products |
18 | * derived from this software without specific prior written permission. |
18 | * derived from this software without specific prior written permission. |
19 | * |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | 31 | ||
32 | /** @addtogroup rd |
32 | /** @addtogroup rd |
33 | * @{ |
33 | * @{ |
34 | */ |
34 | */ |
35 | 35 | ||
36 | /** |
36 | /** |
37 | * @file rd.c |
37 | * @file rd.c |
38 | * @brief Initial RAM disk for HelenOS. |
38 | * @brief Initial RAM disk for HelenOS. |
39 | */ |
39 | */ |
40 | 40 | ||
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <ipc/services.h> |
42 | #include <ipc/services.h> |
43 | #include <ipc/ns.h> |
43 | #include <ipc/ns.h> |
44 | #include <sysinfo.h> |
44 | #include <sysinfo.h> |
45 | #include <as.h> |
45 | #include <as.h> |
46 | #include <ddi.h> |
46 | #include <ddi.h> |
47 | #include <align.h> |
47 | #include <align.h> |
48 | #include <bool.h> |
48 | #include <bool.h> |
49 | #include <errno.h> |
49 | #include <errno.h> |
50 | #include <async.h> |
50 | #include <async.h> |
51 | #include <align.h> |
51 | #include <align.h> |
52 | #include <async.h> |
52 | #include <async.h> |
53 | #include <futex.h> |
53 | #include <futex.h> |
54 | #include "rd.h" |
54 | #include "rd.h" |
55 | 55 | ||
56 | /** Pointer to the ramdisk's image. */ |
56 | /** Pointer to the ramdisk's image. */ |
57 | static void *rd_addr; |
57 | static void *rd_addr; |
58 | /** Size of the ramdisk. */ |
58 | /** Size of the ramdisk. */ |
59 | static size_t rd_size; |
59 | static size_t rd_size; |
60 | 60 | ||
61 | /** |
61 | /** |
62 | * This futex protects the ramdisk's data. |
62 | * This futex protects the ramdisk's data. |
63 | * If we were to serve multiple requests (read + write or several writes) |
63 | * If we were to serve multiple requests (read + write or several writes) |
64 | * concurrently (i.e. from two or more threads), each read and write needs to be |
64 | * concurrently (i.e. from two or more threads), each read and write needs to be |
65 | * protected by this futex. |
65 | * protected by this futex. |
66 | */ |
66 | */ |
67 | atomic_t rd_futex = FUTEX_INITIALIZER; |
67 | atomic_t rd_futex = FUTEX_INITIALIZER; |
68 | 68 | ||
69 | /** Handle one connection to ramdisk. |
69 | /** Handle one connection to ramdisk. |
70 | * |
70 | * |
71 | * @param iid Hash of the request that opened the connection. |
71 | * @param iid Hash of the request that opened the connection. |
72 | * @param icall Call data of the request that opened the connection. |
72 | * @param icall Call data of the request that opened the connection. |
73 | */ |
73 | */ |
74 | static void rd_connection(ipc_callid_t iid, ipc_call_t *icall) |
74 | static void rd_connection(ipc_callid_t iid, ipc_call_t *icall) |
75 | { |
75 | { |
76 | ipc_callid_t callid; |
76 | ipc_callid_t callid; |
77 | ipc_call_t call; |
77 | ipc_call_t call; |
78 | int retval; |
78 | int retval; |
79 | void *fs_va = NULL; |
79 | void *fs_va = NULL; |
80 | ipcarg_t offset; |
80 | ipcarg_t offset; |
81 | 81 | ||
82 | /* |
82 | /* |
83 | * We allocate VA for communication per connection. |
83 | * We allocate VA for communication per connection. |
84 | * This allows us to potentionally have more clients and work |
84 | * This allows us to potentionally have more clients and work |
85 | * concurrently. |
85 | * concurrently. |
86 | */ |
86 | */ |
87 | fs_va = as_get_mappable_page(ALIGN_UP(BLOCK_SIZE, PAGE_SIZE)); |
87 | fs_va = as_get_mappable_page(ALIGN_UP(BLOCK_SIZE, PAGE_SIZE)); |
88 | if (!fs_va) { |
88 | if (!fs_va) { |
89 | /* |
89 | /* |
90 | * Hang up the phone if we cannot proceed any further. |
90 | * Hang up the phone if we cannot proceed any further. |
91 | * This is the answer to the call that opened the connection. |
91 | * This is the answer to the call that opened the connection. |
92 | */ |
92 | */ |
93 | ipc_answer_fast(iid, EHANGUP, 0, 0); |
93 | ipc_answer_fast(iid, EHANGUP, 0, 0); |
94 | return; |
94 | return; |
95 | } else { |
95 | } else { |
96 | /* |
96 | /* |
97 | * Answer the first IPC_M_CONNECT_ME_TO call. |
97 | * Answer the first IPC_M_CONNECT_ME_TO call. |
98 | * Return supported block size as ARG1. |
98 | * Return supported block size as ARG1. |
99 | */ |
99 | */ |
100 | ipc_answer_fast(iid, EOK, BLOCK_SIZE, 0); |
100 | ipc_answer_fast(iid, EOK, BLOCK_SIZE, 0); |
101 | } |
101 | } |
102 | 102 | ||
103 | /* |
103 | /* |
104 | * Now we wait for the client to send us its communication as_area. |
104 | * Now we wait for the client to send us its communication as_area. |
105 | */ |
105 | */ |
106 | callid = async_get_call(&call); |
106 | callid = async_get_call(&call); |
107 | if (IPC_GET_METHOD(call) == IPC_M_AS_AREA_SEND) { |
107 | if (IPC_GET_METHOD(call) == IPC_M_AS_AREA_SEND) { |
108 | if (IPC_GET_ARG1(call) >= (ipcarg_t) BLOCK_SIZE) { |
108 | if (IPC_GET_ARG1(call) >= (ipcarg_t) BLOCK_SIZE) { |
109 | /* |
109 | /* |
110 | * The client sends an as_area that can absorb the whole |
110 | * The client sends an as_area that can absorb the whole |
111 | * block. |
111 | * block. |
112 | */ |
112 | */ |
113 | ipc_answer_fast(callid, EOK, (uintptr_t) fs_va, 0); |
113 | ipc_answer_fast(callid, EOK, (uintptr_t) fs_va, 0); |
114 | } else { |
114 | } else { |
115 | /* |
115 | /* |
116 | * The client offered as_area too small. |
116 | * The client offered as_area too small. |
117 | * Close the connection. |
117 | * Close the connection. |
118 | */ |
118 | */ |
119 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
119 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
120 | return; |
120 | return; |
121 | } |
121 | } |
122 | } else { |
122 | } else { |
123 | /* |
123 | /* |
124 | * The client doesn't speak the same protocol. |
124 | * The client doesn't speak the same protocol. |
125 | * At this point we can't handle protocol variations. |
125 | * At this point we can't handle protocol variations. |
126 | * Close the connection. |
126 | * Close the connection. |
127 | */ |
127 | */ |
128 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
128 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
129 | return; |
129 | return; |
130 | } |
130 | } |
131 | 131 | ||
132 | while (1) { |
132 | while (1) { |
133 | callid = async_get_call(&call); |
133 | callid = async_get_call(&call); |
134 | switch (IPC_GET_METHOD(call)) { |
134 | switch (IPC_GET_METHOD(call)) { |
135 | case IPC_M_PHONE_HUNGUP: |
135 | case IPC_M_PHONE_HUNGUP: |
136 | /* |
136 | /* |
137 | * The other side has hung up. |
137 | * The other side has hung up. |
138 | * Answer the message and exit the pseudo thread. |
138 | * Answer the message and exit the fibril. |
139 | */ |
139 | */ |
140 | ipc_answer_fast(callid, EOK, 0, 0); |
140 | ipc_answer_fast(callid, EOK, 0, 0); |
141 | return; |
141 | return; |
142 | case RD_READ_BLOCK: |
142 | case RD_READ_BLOCK: |
143 | offset = IPC_GET_ARG1(call); |
143 | offset = IPC_GET_ARG1(call); |
144 | if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
144 | if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
145 | /* |
145 | /* |
146 | * Reading past the end of the device. |
146 | * Reading past the end of the device. |
147 | */ |
147 | */ |
148 | retval = ELIMIT; |
148 | retval = ELIMIT; |
149 | break; |
149 | break; |
150 | } |
150 | } |
151 | futex_down(&rd_futex); |
151 | futex_down(&rd_futex); |
152 | memcpy(fs_va, rd_addr + offset, BLOCK_SIZE); |
152 | memcpy(fs_va, rd_addr + offset, BLOCK_SIZE); |
153 | futex_up(&rd_futex); |
153 | futex_up(&rd_futex); |
154 | retval = EOK; |
154 | retval = EOK; |
155 | break; |
155 | break; |
156 | case RD_WRITE_BLOCK: |
156 | case RD_WRITE_BLOCK: |
157 | offset = IPC_GET_ARG1(call); |
157 | offset = IPC_GET_ARG1(call); |
158 | if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
158 | if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) { |
159 | /* |
159 | /* |
160 | * Writing past the end of the device. |
160 | * Writing past the end of the device. |
161 | */ |
161 | */ |
162 | retval = ELIMIT; |
162 | retval = ELIMIT; |
163 | break; |
163 | break; |
164 | } |
164 | } |
165 | futex_up(&rd_futex); |
165 | futex_up(&rd_futex); |
166 | memcpy(rd_addr + offset, fs_va, BLOCK_SIZE); |
166 | memcpy(rd_addr + offset, fs_va, BLOCK_SIZE); |
167 | futex_down(&rd_futex); |
167 | futex_down(&rd_futex); |
168 | retval = EOK; |
168 | retval = EOK; |
169 | break; |
169 | break; |
170 | default: |
170 | default: |
171 | /* |
171 | /* |
172 | * The client doesn't speak the same protocol. |
172 | * The client doesn't speak the same protocol. |
173 | * Instead of closing the connection, we just ignore |
173 | * Instead of closing the connection, we just ignore |
174 | * the call. This can be useful if the client uses a |
174 | * the call. This can be useful if the client uses a |
175 | * newer version of the protocol. |
175 | * newer version of the protocol. |
176 | */ |
176 | */ |
177 | retval = EINVAL; |
177 | retval = EINVAL; |
178 | break; |
178 | break; |
179 | } |
179 | } |
180 | ipc_answer_fast(callid, retval, 0, 0); |
180 | ipc_answer_fast(callid, retval, 0, 0); |
181 | } |
181 | } |
182 | } |
182 | } |
183 | 183 | ||
184 | /** Prepare the ramdisk image for operation. */ |
184 | /** Prepare the ramdisk image for operation. */ |
185 | static bool rd_init(void) |
185 | static bool rd_init(void) |
186 | { |
186 | { |
187 | int retval, flags; |
187 | int retval, flags; |
188 | 188 | ||
189 | rd_size = sysinfo_value("rd.size"); |
189 | rd_size = sysinfo_value("rd.size"); |
190 | void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
190 | void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
191 | 191 | ||
192 | if (rd_size == 0) |
192 | if (rd_size == 0) |
193 | return false; |
193 | return false; |
194 | 194 | ||
195 | rd_addr = as_get_mappable_page(rd_size); |
195 | rd_addr = as_get_mappable_page(rd_size); |
196 | 196 | ||
197 | flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
197 | flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
198 | retval = physmem_map(rd_ph_addr, rd_addr, |
198 | retval = physmem_map(rd_ph_addr, rd_addr, |
199 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
199 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
200 | 200 | ||
201 | if (retval < 0) |
201 | if (retval < 0) |
202 | return false; |
202 | return false; |
203 | return true; |
203 | return true; |
204 | } |
204 | } |
205 | 205 | ||
206 | int main(int argc, char **argv) |
206 | int main(int argc, char **argv) |
207 | { |
207 | { |
208 | if (rd_init()) { |
208 | if (rd_init()) { |
209 | ipcarg_t phonead; |
209 | ipcarg_t phonead; |
210 | 210 | ||
211 | async_set_client_connection(rd_connection); |
211 | async_set_client_connection(rd_connection); |
212 | 212 | ||
213 | /* Register service at nameserver */ |
213 | /* Register service at nameserver */ |
214 | if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0) |
214 | if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0) |
215 | return -1; |
215 | return -1; |
216 | 216 | ||
217 | async_manager(); |
217 | async_manager(); |
218 | 218 | ||
219 | /* Never reached */ |
219 | /* Never reached */ |
220 | return 0; |
220 | return 0; |
221 | } |
221 | } |
222 | 222 | ||
223 | return -1; |
223 | return -1; |
224 | } |
224 | } |
225 | 225 | ||
226 | /** |
226 | /** |
227 | * @} |
227 | * @} |
228 | */ |
228 | */ |
229 | 229 |