Rev 4425 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4425 | Rev 4439 | ||
---|---|---|---|
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 <stdio.h> |
54 | #include <stdio.h> |
55 | #include <devmap.h> |
55 | #include <devmap.h> |
56 | #include <ipc/bd.h> |
56 | #include <ipc/bd.h> |
57 | 57 | ||
58 | #define NAME "rd" |
58 | #define NAME "rd" |
59 | 59 | ||
60 | /** Pointer to the ramdisk's image. */ |
60 | /** Pointer to the ramdisk's image. */ |
61 | static void *rd_addr; |
61 | static void *rd_addr; |
62 | /** Size of the ramdisk. */ |
62 | /** Size of the ramdisk. */ |
63 | static size_t rd_size; |
63 | static size_t rd_size; |
64 | 64 | ||
65 | /** |
65 | /** |
66 | * This futex protects the ramdisk's data. |
66 | * This futex protects the ramdisk's data. |
67 | * If we were to serve multiple requests (read + write or several writes) |
67 | * If we were to serve multiple requests (read + write or several writes) |
68 | * concurrently (i.e. from two or more threads), each read and write needs to be |
68 | * concurrently (i.e. from two or more threads), each read and write needs to be |
69 | * protected by this futex. |
69 | * protected by this futex. |
70 | */ |
70 | */ |
71 | atomic_t rd_futex = FUTEX_INITIALIZER; |
71 | atomic_t rd_futex = FUTEX_INITIALIZER; |
72 | 72 | ||
73 | /** Handle one connection to ramdisk. |
73 | /** Handle one connection to ramdisk. |
74 | * |
74 | * |
75 | * @param iid Hash of the request that opened the connection. |
75 | * @param iid Hash of the request that opened the connection. |
76 | * @param icall Call data of the request that opened the connection. |
76 | * @param icall Call data of the request that opened the connection. |
77 | */ |
77 | */ |
78 | static void rd_connection(ipc_callid_t iid, ipc_call_t *icall) |
78 | static void rd_connection(ipc_callid_t iid, ipc_call_t *icall) |
79 | { |
79 | { |
80 | ipc_callid_t callid; |
80 | ipc_callid_t callid; |
81 | ipc_call_t call; |
81 | ipc_call_t call; |
82 | int retval; |
82 | int retval; |
83 | void *fs_va = NULL; |
83 | void *fs_va = NULL; |
84 | off_t offset; |
84 | off_t offset; |
85 | size_t block_size; |
85 | size_t block_size; |
86 | size_t maxblock_size; |
86 | size_t maxblock_size; |
87 | 87 | ||
88 | /* |
88 | /* |
89 | * Answer the first IPC_M_CONNECT_ME_TO call. |
89 | * Answer the first IPC_M_CONNECT_ME_TO call. |
90 | */ |
90 | */ |
91 | ipc_answer_0(iid, EOK); |
91 | ipc_answer_0(iid, EOK); |
92 | 92 | ||
93 | /* |
93 | /* |
94 | * Now we wait for the client to send us its communication as_area. |
94 | * Now we wait for the client to send us its communication as_area. |
95 | */ |
95 | */ |
96 | int flags; |
96 | int flags; |
97 | if (ipc_share_out_receive(&callid, &maxblock_size, &flags)) { |
97 | if (ipc_share_out_receive(&callid, &maxblock_size, &flags)) { |
98 | fs_va = as_get_mappable_page(maxblock_size); |
98 | fs_va = as_get_mappable_page(maxblock_size); |
99 | if (fs_va) { |
99 | if (fs_va) { |
100 | (void) ipc_share_out_finalize(callid, fs_va); |
100 | (void) ipc_share_out_finalize(callid, fs_va); |
101 | } else { |
101 | } else { |
102 | ipc_answer_0(callid, EHANGUP); |
102 | ipc_answer_0(callid, EHANGUP); |
103 | return; |
103 | return; |
104 | } |
104 | } |
105 | } else { |
105 | } else { |
106 | /* |
106 | /* |
107 | * The client doesn't speak the same protocol. |
107 | * The client doesn't speak the same protocol. |
108 | * At this point we can't handle protocol variations. |
108 | * At this point we can't handle protocol variations. |
109 | * Close the connection. |
109 | * Close the connection. |
110 | */ |
110 | */ |
111 | ipc_answer_0(callid, EHANGUP); |
111 | ipc_answer_0(callid, EHANGUP); |
112 | return; |
112 | return; |
113 | } |
113 | } |
114 | 114 | ||
115 | while (true) { |
115 | while (true) { |
116 | callid = async_get_call(&call); |
116 | callid = async_get_call(&call); |
117 | switch (IPC_GET_METHOD(call)) { |
117 | switch (IPC_GET_METHOD(call)) { |
118 | case IPC_M_PHONE_HUNGUP: |
118 | case IPC_M_PHONE_HUNGUP: |
119 | /* |
119 | /* |
120 | * The other side has hung up. |
120 | * The other side has hung up. |
121 | * Answer the message and exit the fibril. |
121 | * Answer the message and exit the fibril. |
122 | */ |
122 | */ |
123 | ipc_answer_0(callid, EOK); |
123 | ipc_answer_0(callid, EOK); |
124 | return; |
124 | return; |
125 | case BD_READ_BLOCK: |
125 | case BD_READ_BLOCK: |
126 | offset = IPC_GET_ARG1(call); |
126 | offset = IPC_GET_ARG1(call); |
127 | block_size = IPC_GET_ARG2(call); |
127 | block_size = IPC_GET_ARG2(call); |
128 | if (block_size > maxblock_size) { |
128 | if (block_size > maxblock_size) { |
129 | /* |
129 | /* |
130 | * Maximum block size exceeded. |
130 | * Maximum block size exceeded. |
131 | */ |
131 | */ |
132 | retval = ELIMIT; |
132 | retval = ELIMIT; |
133 | break; |
133 | break; |
134 | } |
134 | } |
135 | if (offset * block_size > rd_size - block_size) { |
135 | if (offset * block_size > rd_size - block_size) { |
136 | /* |
136 | /* |
137 | * Reading past the end of the device. |
137 | * Reading past the end of the device. |
138 | */ |
138 | */ |
139 | retval = ELIMIT; |
139 | retval = ELIMIT; |
140 | break; |
140 | break; |
141 | } |
141 | } |
142 | futex_down(&rd_futex); |
142 | futex_down(&rd_futex); |
143 | memcpy(fs_va, rd_addr + offset * block_size, block_size); |
143 | memcpy(fs_va, rd_addr + offset * block_size, block_size); |
144 | futex_up(&rd_futex); |
144 | futex_up(&rd_futex); |
145 | retval = EOK; |
145 | retval = EOK; |
146 | break; |
146 | break; |
147 | case BD_WRITE_BLOCK: |
147 | case BD_WRITE_BLOCK: |
148 | offset = IPC_GET_ARG1(call); |
148 | offset = IPC_GET_ARG1(call); |
149 | block_size = IPC_GET_ARG2(call); |
149 | block_size = IPC_GET_ARG2(call); |
150 | if (block_size > maxblock_size) { |
150 | if (block_size > maxblock_size) { |
151 | /* |
151 | /* |
152 | * Maximum block size exceeded. |
152 | * Maximum block size exceeded. |
153 | */ |
153 | */ |
154 | retval = ELIMIT; |
154 | retval = ELIMIT; |
155 | break; |
155 | break; |
156 | } |
156 | } |
157 | if (offset * block_size > rd_size - block_size) { |
157 | if (offset * block_size > rd_size - block_size) { |
158 | /* |
158 | /* |
159 | * Writing past the end of the device. |
159 | * Writing past the end of the device. |
160 | */ |
160 | */ |
161 | retval = ELIMIT; |
161 | retval = ELIMIT; |
162 | break; |
162 | break; |
163 | } |
163 | } |
164 | futex_up(&rd_futex); |
164 | futex_up(&rd_futex); |
165 | memcpy(rd_addr + offset * block_size, fs_va, block_size); |
165 | memcpy(rd_addr + offset * block_size, fs_va, block_size); |
166 | futex_down(&rd_futex); |
166 | futex_down(&rd_futex); |
167 | retval = EOK; |
167 | retval = EOK; |
168 | break; |
168 | break; |
169 | default: |
169 | default: |
170 | /* |
170 | /* |
171 | * The client doesn't speak the same protocol. |
171 | * The client doesn't speak the same protocol. |
172 | * Instead of closing the connection, we just ignore |
172 | * Instead of closing the connection, we just ignore |
173 | * the call. This can be useful if the client uses a |
173 | * the call. This can be useful if the client uses a |
174 | * newer version of the protocol. |
174 | * newer version of the protocol. |
175 | */ |
175 | */ |
176 | retval = EINVAL; |
176 | retval = EINVAL; |
177 | break; |
177 | break; |
178 | } |
178 | } |
179 | ipc_answer_0(callid, retval); |
179 | ipc_answer_0(callid, retval); |
180 | } |
180 | } |
181 | } |
181 | } |
182 | 182 | ||
183 | /** Prepare the ramdisk image for operation. */ |
183 | /** Prepare the ramdisk image for operation. */ |
184 | static bool rd_init(void) |
184 | static bool rd_init(void) |
185 | { |
185 | { |
186 | rd_size = sysinfo_value("rd.size"); |
186 | rd_size = sysinfo_value("rd.size"); |
187 | void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
187 | void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical"); |
188 | 188 | ||
189 | if (rd_size == 0) { |
189 | if (rd_size == 0) { |
190 | printf(NAME ": No RAM disk found\n"); |
190 | printf(NAME ": No RAM disk found\n"); |
191 | return false; |
191 | return false; |
192 | } |
192 | } |
193 | 193 | ||
194 | rd_addr = as_get_mappable_page(rd_size); |
194 | rd_addr = as_get_mappable_page(rd_size); |
195 | 195 | ||
196 | int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
196 | int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE; |
197 | int retval = physmem_map(rd_ph_addr, rd_addr, |
197 | int retval = physmem_map(rd_ph_addr, rd_addr, |
198 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
198 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags); |
199 | 199 | ||
200 | if (retval < 0) { |
200 | if (retval < 0) { |
201 | printf(NAME ": Error mapping RAM disk\n"); |
201 | printf(NAME ": Error mapping RAM disk\n"); |
202 | return false; |
202 | return false; |
203 | } |
203 | } |
204 | 204 | ||
205 | printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size); |
205 | printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size); |
206 | 206 | ||
207 | int rc = devmap_driver_register(NAME, rd_connection); |
207 | int rc = devmap_driver_register(NAME, rd_connection); |
208 | if (rc < 0) { |
208 | if (rc < 0) { |
209 | printf(NAME ": Unable to register driver (%d)\n", rc); |
209 | printf(NAME ": Unable to register driver (%d)\n", rc); |
210 | return false; |
210 | return false; |
211 | } |
211 | } |
212 | 212 | ||
213 | dev_handle_t dev_handle; |
213 | dev_handle_t dev_handle; |
214 | if (devmap_device_register("initrd", &dev_handle) != EOK) { |
214 | if (devmap_device_register("initrd", &dev_handle) != EOK) { |
215 | devmap_hangup_phone(DEVMAP_DRIVER); |
215 | devmap_hangup_phone(DEVMAP_DRIVER); |
216 | printf(NAME ": Unable to register device\n"); |
216 | printf(NAME ": Unable to register device\n"); |
217 | return false; |
217 | return false; |
218 | } |
218 | } |
219 | 219 | ||
220 | return true; |
220 | return true; |
221 | } |
221 | } |
222 | 222 | ||
223 | int main(int argc, char **argv) |
223 | int main(int argc, char **argv) |
224 | { |
224 | { |
225 | printf(NAME ": HelenOS RAM disk server\n"); |
225 | printf(NAME ": HelenOS RAM disk server\n"); |
226 | 226 | ||
227 | if (!rd_init()) |
227 | if (!rd_init()) |
228 | return -1; |
228 | return -1; |
229 | 229 | ||
230 | printf(NAME ": Accepting connections\n"); |
230 | printf(NAME ": Accepting connections\n"); |
231 | async_manager(); |
231 | async_manager(); |
232 | 232 | ||
233 | /* Never reached */ |
233 | /* Never reached */ |
234 | return 0; |
234 | return 0; |
235 | } |
235 | } |
236 | 236 | ||
237 | /** |
237 | /** |
238 | * @} |
238 | * @} |
239 | */ |
239 | */ |
240 | 240 |