[228b135] | 1 | /*
|
---|
[ff3a34b] | 2 | * Copyright (c) 2007 Michal Konopa
|
---|
| 3 | * Copyright (c) 2007 Martin Jelen
|
---|
| 4 | * Copyright (c) 2007 Peter Majer
|
---|
[84947a4] | 5 | * Copyright (c) 2007 Jakub Jermar
|
---|
[228b135] | 6 | * All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
---|
| 9 | * modification, are permitted provided that the following conditions
|
---|
| 10 | * are met:
|
---|
| 11 | *
|
---|
| 12 | * - Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 18 | * derived from this software without specific prior written permission.
|
---|
| 19 | *
|
---|
| 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
|
---|
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | /** @addtogroup rd
|
---|
| 33 | * @{
|
---|
[d9fae235] | 34 | */
|
---|
[228b135] | 35 |
|
---|
| 36 | /**
|
---|
[d9fae235] | 37 | * @file rd.c
|
---|
| 38 | * @brief Initial RAM disk for HelenOS.
|
---|
[228b135] | 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <ipc/services.h>
|
---|
| 42 | #include <ipc/ns.h>
|
---|
[7c34822e] | 43 | #include <sysinfo.h>
|
---|
| 44 | #include <as.h>
|
---|
| 45 | #include <ddi.h>
|
---|
| 46 | #include <align.h>
|
---|
| 47 | #include <bool.h>
|
---|
[228b135] | 48 | #include <errno.h>
|
---|
| 49 | #include <async.h>
|
---|
[ff3a34b] | 50 | #include <align.h>
|
---|
| 51 | #include <async.h>
|
---|
[1e4cada] | 52 | #include <fibril_synch.h>
|
---|
[2f65fb0] | 53 | #include <stdio.h>
|
---|
[15f3c3f] | 54 | #include <loc.h>
|
---|
[c5747fe] | 55 | #include <ipc/bd.h>
|
---|
[1ee00b7] | 56 | #include <macros.h>
|
---|
[b5daa89] | 57 | #include <inttypes.h>
|
---|
[228b135] | 58 |
|
---|
[b5daa89] | 59 | #define NAME "rd"
|
---|
[2f65fb0] | 60 |
|
---|
[1ee00b7] | 61 | /** Pointer to the ramdisk's image */
|
---|
[ff3a34b] | 62 | static void *rd_addr;
|
---|
[d9fae235] | 63 |
|
---|
[1ee00b7] | 64 | /** Size of the ramdisk */
|
---|
[3ae470a] | 65 | static size_t rd_size;
|
---|
[228b135] | 66 |
|
---|
[1ee00b7] | 67 | /** Block size */
|
---|
| 68 | static const size_t block_size = 512;
|
---|
| 69 |
|
---|
| 70 | static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf);
|
---|
| 71 | static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
|
---|
| 72 |
|
---|
[d9fae235] | 73 | /** This rwlock protects the ramdisk's data.
|
---|
| 74 | *
|
---|
[84947a4] | 75 | * If we were to serve multiple requests (read + write or several writes)
|
---|
[d9fae235] | 76 | * concurrently (i.e. from two or more threads), each read and write needs to
|
---|
| 77 | * be protected by this rwlock.
|
---|
| 78 | *
|
---|
| 79 | */
|
---|
[52e4f526] | 80 | fibril_rwlock_t rd_lock;
|
---|
[84947a4] | 81 |
|
---|
| 82 | /** Handle one connection to ramdisk.
|
---|
| 83 | *
|
---|
[d9fae235] | 84 | * @param iid Hash of the request that opened the connection.
|
---|
| 85 | * @param icall Call data of the request that opened the connection.
|
---|
[84947a4] | 86 | */
|
---|
[9934f7d] | 87 | static void rd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[228b135] | 88 | {
|
---|
| 89 | ipc_callid_t callid;
|
---|
| 90 | ipc_call_t call;
|
---|
| 91 | int retval;
|
---|
[3ae470a] | 92 | void *fs_va = NULL;
|
---|
[1ee00b7] | 93 | uint64_t ba;
|
---|
| 94 | size_t cnt;
|
---|
| 95 | size_t comm_size;
|
---|
[d9fae235] | 96 |
|
---|
[84947a4] | 97 | /*
|
---|
[c544c5d] | 98 | * Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
[84947a4] | 99 | */
|
---|
[ffa2c8ef] | 100 | async_answer_0(iid, EOK);
|
---|
[d9fae235] | 101 |
|
---|
[84947a4] | 102 | /*
|
---|
| 103 | * Now we wait for the client to send us its communication as_area.
|
---|
| 104 | */
|
---|
[47b7006] | 105 | unsigned int flags;
|
---|
[0da4e41] | 106 | if (async_share_out_receive(&callid, &comm_size, &flags)) {
|
---|
[fbcdeb8] | 107 | (void) async_share_out_finalize(callid, &fs_va);
|
---|
| 108 | if (fs_va == (void *) -1) {
|
---|
[ffa2c8ef] | 109 | async_answer_0(callid, EHANGUP);
|
---|
[d9fae235] | 110 | return;
|
---|
[84947a4] | 111 | }
|
---|
| 112 | } else {
|
---|
| 113 | /*
|
---|
| 114 | * The client doesn't speak the same protocol.
|
---|
| 115 | * At this point we can't handle protocol variations.
|
---|
| 116 | * Close the connection.
|
---|
| 117 | */
|
---|
[ffa2c8ef] | 118 | async_answer_0(callid, EHANGUP);
|
---|
[84947a4] | 119 | return;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[cb41a5e] | 122 | while (true) {
|
---|
[228b135] | 123 | callid = async_get_call(&call);
|
---|
[79ae36dd] | 124 |
|
---|
| 125 | if (!IPC_GET_IMETHOD(call)) {
|
---|
[84947a4] | 126 | /*
|
---|
| 127 | * The other side has hung up.
|
---|
[79ae36dd] | 128 | * Exit the fibril.
|
---|
[84947a4] | 129 | */
|
---|
[ffa2c8ef] | 130 | async_answer_0(callid, EOK);
|
---|
[8b243f2] | 131 | return;
|
---|
[79ae36dd] | 132 | }
|
---|
| 133 |
|
---|
| 134 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[1ee00b7] | 135 | case BD_READ_BLOCKS:
|
---|
| 136 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
| 137 | IPC_GET_ARG2(call));
|
---|
| 138 | cnt = IPC_GET_ARG3(call);
|
---|
| 139 | if (cnt * block_size > comm_size) {
|
---|
[3ae470a] | 140 | retval = ELIMIT;
|
---|
| 141 | break;
|
---|
| 142 | }
|
---|
[1ee00b7] | 143 | retval = rd_read_blocks(ba, cnt, fs_va);
|
---|
[84947a4] | 144 | break;
|
---|
[1ee00b7] | 145 | case BD_WRITE_BLOCKS:
|
---|
| 146 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
| 147 | IPC_GET_ARG2(call));
|
---|
| 148 | cnt = IPC_GET_ARG3(call);
|
---|
| 149 | if (cnt * block_size > comm_size) {
|
---|
[c544c5d] | 150 | retval = ELIMIT;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
[1ee00b7] | 153 | retval = rd_write_blocks(ba, cnt, fs_va);
|
---|
[8b243f2] | 154 | break;
|
---|
[1ee00b7] | 155 | case BD_GET_BLOCK_SIZE:
|
---|
[ffa2c8ef] | 156 | async_answer_1(callid, EOK, block_size);
|
---|
[1ee00b7] | 157 | continue;
|
---|
[08232ee] | 158 | case BD_GET_NUM_BLOCKS:
|
---|
[ffa2c8ef] | 159 | async_answer_2(callid, EOK, LOWER32(rd_size / block_size),
|
---|
[08232ee] | 160 | UPPER32(rd_size / block_size));
|
---|
| 161 | continue;
|
---|
[8b243f2] | 162 | default:
|
---|
[84947a4] | 163 | /*
|
---|
| 164 | * The client doesn't speak the same protocol.
|
---|
| 165 | * Instead of closing the connection, we just ignore
|
---|
| 166 | * the call. This can be useful if the client uses a
|
---|
| 167 | * newer version of the protocol.
|
---|
| 168 | */
|
---|
[8b243f2] | 169 | retval = EINVAL;
|
---|
[84947a4] | 170 | break;
|
---|
[228b135] | 171 | }
|
---|
[ffa2c8ef] | 172 | async_answer_0(callid, retval);
|
---|
[8b243f2] | 173 | }
|
---|
[228b135] | 174 | }
|
---|
| 175 |
|
---|
[1ee00b7] | 176 | /** Read blocks from the device. */
|
---|
| 177 | static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
|
---|
| 178 | {
|
---|
| 179 | if ((ba + cnt) * block_size > rd_size) {
|
---|
| 180 | /* Reading past the end of the device. */
|
---|
| 181 | return ELIMIT;
|
---|
| 182 | }
|
---|
[d9fae235] | 183 |
|
---|
[1ee00b7] | 184 | fibril_rwlock_read_lock(&rd_lock);
|
---|
| 185 | memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
|
---|
| 186 | fibril_rwlock_read_unlock(&rd_lock);
|
---|
[d9fae235] | 187 |
|
---|
[1ee00b7] | 188 | return EOK;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /** Write blocks to the device. */
|
---|
| 192 | static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
|
---|
| 193 | {
|
---|
| 194 | if ((ba + cnt) * block_size > rd_size) {
|
---|
| 195 | /* Writing past the end of the device. */
|
---|
| 196 | return ELIMIT;
|
---|
| 197 | }
|
---|
[d9fae235] | 198 |
|
---|
[1ee00b7] | 199 | fibril_rwlock_write_lock(&rd_lock);
|
---|
| 200 | memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
|
---|
| 201 | fibril_rwlock_write_unlock(&rd_lock);
|
---|
[d9fae235] | 202 |
|
---|
[1ee00b7] | 203 | return EOK;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[84947a4] | 206 | /** Prepare the ramdisk image for operation. */
|
---|
[7c34822e] | 207 | static bool rd_init(void)
|
---|
[228b135] | 208 | {
|
---|
[b5daa89] | 209 | sysarg_t size;
|
---|
| 210 | int ret = sysinfo_get_value("rd.size", &size);
|
---|
| 211 | if ((ret != EOK) || (size == 0)) {
|
---|
[d9fae235] | 212 | printf("%s: No RAM disk found\n", NAME);
|
---|
| 213 | return false;
|
---|
| 214 | }
|
---|
[7c34822e] | 215 |
|
---|
[b5daa89] | 216 | sysarg_t addr_phys;
|
---|
| 217 | ret = sysinfo_get_value("rd.address.physical", &addr_phys);
|
---|
| 218 | if ((ret != EOK) || (addr_phys == 0)) {
|
---|
[d9fae235] | 219 | printf("%s: Invalid RAM disk physical address\n", NAME);
|
---|
[7c34822e] | 220 | return false;
|
---|
[2f65fb0] | 221 | }
|
---|
[228b135] | 222 |
|
---|
[b5daa89] | 223 | rd_size = ALIGN_UP(size, block_size);
|
---|
| 224 | unsigned int flags =
|
---|
| 225 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
|
---|
[fbcdeb8] | 226 |
|
---|
| 227 | ret = physmem_map((void *) addr_phys,
|
---|
| 228 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags, &rd_addr);
|
---|
| 229 | if (ret != EOK) {
|
---|
[d9fae235] | 230 | printf("%s: Error mapping RAM disk\n", NAME);
|
---|
[2f65fb0] | 231 | return false;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[b5daa89] | 234 | printf("%s: Found RAM disk at %p, %" PRIun " bytes\n", NAME,
|
---|
| 235 | (void *) addr_phys, size);
|
---|
[2f65fb0] | 236 |
|
---|
[b5daa89] | 237 | ret = loc_server_register(NAME, rd_connection);
|
---|
| 238 | if (ret < 0) {
|
---|
| 239 | printf("%s: Unable to register driver (%d)\n", NAME, ret);
|
---|
[2f65fb0] | 240 | return false;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[15f3c3f] | 243 | service_id_t service_id;
|
---|
[b5daa89] | 244 | ret = loc_service_register("bd/initrd", &service_id);
|
---|
| 245 | if (ret != EOK) {
|
---|
[15f3c3f] | 246 | printf("%s: Unable to register device service\n", NAME);
|
---|
[537611cc] | 247 | return false;
|
---|
| 248 | }
|
---|
[79ae36dd] | 249 |
|
---|
[52e4f526] | 250 | fibril_rwlock_initialize(&rd_lock);
|
---|
[2f65fb0] | 251 |
|
---|
[7c34822e] | 252 | return true;
|
---|
| 253 | }
|
---|
[228b135] | 254 |
|
---|
[7c34822e] | 255 | int main(int argc, char **argv)
|
---|
| 256 | {
|
---|
[d9fae235] | 257 | printf("%s: HelenOS RAM disk server\n", NAME);
|
---|
[7c34822e] | 258 |
|
---|
[2f65fb0] | 259 | if (!rd_init())
|
---|
| 260 | return -1;
|
---|
| 261 |
|
---|
[d9fae235] | 262 | printf("%s: Accepting connections\n", NAME);
|
---|
[2f65fb0] | 263 | async_manager();
|
---|
[79ae36dd] | 264 |
|
---|
[2f65fb0] | 265 | /* Never reached */
|
---|
| 266 | return 0;
|
---|
[228b135] | 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
| 270 | * @}
|
---|
[cb41a5e] | 271 | */
|
---|