1 #include "inc.h" 2 3 /* Private shm_perm.mode flags, synchronized with NetBSD kernel values */ 4 #define SHM_ALLOC 0x0800 /* slot is in use (SHMSEG_ALLOCATED) */ 5 6 struct shm_struct { 7 struct shmid_ds shmid_ds; 8 vir_bytes page; 9 phys_bytes vm_id; 10 }; 11 static struct shm_struct shm_list[SHMMNI]; 12 static unsigned int shm_list_nr = 0; /* highest in-use slot number plus one */ 13 14 static struct shm_struct * 15 shm_find_key(key_t key) 16 { 17 unsigned int i; 18 19 if (key == IPC_PRIVATE) 20 return NULL; 21 22 for (i = 0; i < shm_list_nr; i++) { 23 if (!(shm_list[i].shmid_ds.shm_perm.mode & SHM_ALLOC)) 24 continue; 25 if (shm_list[i].shmid_ds.shm_perm._key == key) 26 return &shm_list[i]; 27 } 28 29 return NULL; 30 } 31 32 static struct shm_struct * 33 shm_find_id(int id) 34 { 35 struct shm_struct *shm; 36 unsigned int i; 37 38 i = IPCID_TO_IX(id); 39 if (i >= shm_list_nr) 40 return NULL; 41 42 shm = &shm_list[i]; 43 if (!(shm->shmid_ds.shm_perm.mode & SHM_ALLOC)) 44 return NULL; 45 if (shm->shmid_ds.shm_perm._seq != IPCID_TO_SEQ(id)) 46 return NULL; 47 return shm; 48 } 49 50 int 51 do_shmget(message * m) 52 { 53 struct shm_struct *shm; 54 unsigned int i, seq; 55 key_t key; 56 size_t size, old_size; 57 int flag; 58 void *page; 59 60 key = m->m_lc_ipc_shmget.key; 61 old_size = size = m->m_lc_ipc_shmget.size; 62 flag = m->m_lc_ipc_shmget.flag; 63 64 if ((shm = shm_find_key(key)) != NULL) { 65 if (!check_perm(&shm->shmid_ds.shm_perm, m->m_source, flag)) 66 return EACCES; 67 if ((flag & IPC_CREAT) && (flag & IPC_EXCL)) 68 return EEXIST; 69 if (size && shm->shmid_ds.shm_segsz < size) 70 return EINVAL; 71 i = shm - shm_list; 72 } else { /* no key found */ 73 if (!(flag & IPC_CREAT)) 74 return ENOENT; 75 if (size <= 0) 76 return EINVAL; 77 size = roundup(size, PAGE_SIZE); 78 if (size <= 0) 79 return EINVAL; 80 81 /* Find a free entry. */ 82 for (i = 0; i < __arraycount(shm_list); i++) 83 if (!(shm_list[i].shmid_ds.shm_perm.mode & SHM_ALLOC)) 84 break; 85 if (i == __arraycount(shm_list)) 86 return ENOSPC; 87 88 /* 89 * Allocate memory to share. For now, we store the page 90 * reference as a numerical value so as to avoid issues with 91 * live update. TODO: a proper solution. 92 */ 93 page = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); 94 if (page == MAP_FAILED) 95 return ENOMEM; 96 memset(page, 0, size); 97 98 /* Initialize the entry. */ 99 shm = &shm_list[i]; 100 seq = shm->shmid_ds.shm_perm._seq; 101 memset(shm, 0, sizeof(*shm)); 102 103 shm->shmid_ds.shm_perm._key = key; 104 shm->shmid_ds.shm_perm.cuid = 105 shm->shmid_ds.shm_perm.uid = getnuid(m->m_source); 106 shm->shmid_ds.shm_perm.cgid = 107 shm->shmid_ds.shm_perm.gid = getngid(m->m_source); 108 shm->shmid_ds.shm_perm.mode = SHM_ALLOC | (flag & ACCESSPERMS); 109 shm->shmid_ds.shm_perm._seq = (seq + 1) & 0x7fff; 110 shm->shmid_ds.shm_segsz = old_size; 111 shm->shmid_ds.shm_atime = 0; 112 shm->shmid_ds.shm_dtime = 0; 113 shm->shmid_ds.shm_ctime = clock_time(NULL); 114 shm->shmid_ds.shm_cpid = getnpid(m->m_source); 115 shm->shmid_ds.shm_lpid = 0; 116 shm->shmid_ds.shm_nattch = 0; 117 shm->page = (vir_bytes)page; 118 shm->vm_id = vm_getphys(sef_self(), page); 119 120 assert(i <= shm_list_nr); 121 if (i == shm_list_nr) 122 shm_list_nr++; 123 } 124 125 m->m_lc_ipc_shmget.retid = IXSEQ_TO_IPCID(i, shm->shmid_ds.shm_perm); 126 return OK; 127 } 128 129 int 130 do_shmat(message * m) 131 { 132 int id, flag, mask; 133 vir_bytes addr; 134 void *ret; 135 struct shm_struct *shm; 136 137 id = m->m_lc_ipc_shmat.id; 138 addr = (vir_bytes)m->m_lc_ipc_shmat.addr; 139 flag = m->m_lc_ipc_shmat.flag; 140 141 if (addr % PAGE_SIZE) { 142 if (flag & SHM_RND) 143 addr -= addr % PAGE_SIZE; 144 else 145 return EINVAL; 146 } 147 148 if ((shm = shm_find_id(id)) == NULL) 149 return EINVAL; 150 151 mask = 0; 152 if (flag & SHM_RDONLY) 153 mask = IPC_R; 154 else 155 mask = IPC_R | IPC_W; 156 if (!check_perm(&shm->shmid_ds.shm_perm, m->m_source, mask)) 157 return EACCES; 158 159 ret = vm_remap(m->m_source, sef_self(), (void *)addr, 160 (void *)shm->page, shm->shmid_ds.shm_segsz); 161 if (ret == MAP_FAILED) 162 return ENOMEM; 163 164 shm->shmid_ds.shm_atime = clock_time(NULL); 165 shm->shmid_ds.shm_lpid = getnpid(m->m_source); 166 /* nattch is updated lazily */ 167 168 m->m_lc_ipc_shmat.retaddr = ret; 169 return OK; 170 } 171 172 void 173 update_refcount_and_destroy(void) 174 { 175 u8_t rc; 176 unsigned int i; 177 178 for (i = 0; i < shm_list_nr; i++) { 179 if (!(shm_list[i].shmid_ds.shm_perm.mode & SHM_ALLOC)) 180 continue; 181 182 rc = vm_getrefcount(sef_self(), (void *)shm_list[i].page); 183 if (rc == (u8_t)-1) { 184 printf("IPC: can't find physical region.\n"); 185 continue; 186 } 187 shm_list[i].shmid_ds.shm_nattch = rc - 1; 188 189 if (shm_list[i].shmid_ds.shm_nattch == 0 && 190 (shm_list[i].shmid_ds.shm_perm.mode & SHM_DEST)) { 191 munmap((void *)shm_list[i].page, 192 roundup(shm_list[i].shmid_ds.shm_segsz, 193 PAGE_SIZE)); 194 /* Mark the entry as free. */ 195 shm_list[i].shmid_ds.shm_perm.mode &= ~SHM_ALLOC; 196 } 197 } 198 199 /* 200 * Now that we may have removed an arbitrary set of slots, ensure that 201 * shm_list_nr again equals the highest in-use slot number plus one. 202 */ 203 while (shm_list_nr > 0 && 204 !(shm_list[shm_list_nr - 1].shmid_ds.shm_perm.mode & SHM_ALLOC)) 205 shm_list_nr--; 206 } 207 208 int 209 do_shmdt(message * m) 210 { 211 struct shm_struct *shm; 212 vir_bytes addr; 213 phys_bytes vm_id; 214 unsigned int i; 215 216 addr = (vir_bytes)m->m_lc_ipc_shmdt.addr; 217 218 if ((vm_id = vm_getphys(m->m_source, (void *)addr)) == 0) 219 return EINVAL; 220 221 for (i = 0; i < shm_list_nr; i++) { 222 shm = &shm_list[i]; 223 224 if (!(shm->shmid_ds.shm_perm.mode & SHM_ALLOC)) 225 continue; 226 227 if (shm->vm_id == vm_id) { 228 shm->shmid_ds.shm_atime = clock_time(NULL); 229 shm->shmid_ds.shm_lpid = getnpid(m->m_source); 230 /* nattch is updated lazily */ 231 232 vm_unmap(m->m_source, (void *)addr); 233 break; 234 } 235 } 236 if (i == shm_list_nr) 237 printf("IPC: do_shmdt: ID %lu not found\n", vm_id); 238 239 update_refcount_and_destroy(); 240 241 return OK; 242 } 243 244 int 245 do_shmctl(message * m) 246 { 247 struct shmid_ds tmp_ds; 248 struct shm_struct *shm; 249 struct shminfo sinfo; 250 struct shm_info s_info; 251 vir_bytes buf; 252 unsigned int i; 253 uid_t uid; 254 int r, id, cmd; 255 256 id = m->m_lc_ipc_shmctl.id; 257 cmd = m->m_lc_ipc_shmctl.cmd; 258 buf = (vir_bytes)m->m_lc_ipc_shmctl.buf; 259 260 /* 261 * For stat calls, sure that all information is up-to-date. Since this 262 * may free the slot, do this before mapping from ID to slot below. 263 */ 264 if (cmd == IPC_STAT || cmd == SHM_STAT) 265 update_refcount_and_destroy(); 266 267 switch (cmd) { 268 case IPC_INFO: 269 case SHM_INFO: 270 shm = NULL; 271 break; 272 case SHM_STAT: 273 if (id < 0 || (unsigned int)id >= shm_list_nr) 274 return EINVAL; 275 shm = &shm_list[id]; 276 if (!(shm->shmid_ds.shm_perm.mode & SHM_ALLOC)) 277 return EINVAL; 278 break; 279 default: 280 if ((shm = shm_find_id(id)) == NULL) 281 return EINVAL; 282 break; 283 } 284 285 switch (cmd) { 286 case IPC_STAT: 287 case SHM_STAT: 288 /* Check whether the caller has read permission. */ 289 if (!check_perm(&shm->shmid_ds.shm_perm, m->m_source, IPC_R)) 290 return EACCES; 291 if ((r = sys_datacopy(SELF, (vir_bytes)&shm->shmid_ds, 292 m->m_source, buf, sizeof(shm->shmid_ds))) != OK) 293 return r; 294 if (cmd == SHM_STAT) 295 m->m_lc_ipc_shmctl.ret = 296 IXSEQ_TO_IPCID(id, shm->shmid_ds.shm_perm); 297 break; 298 case IPC_SET: 299 uid = getnuid(m->m_source); 300 if (uid != shm->shmid_ds.shm_perm.cuid && 301 uid != shm->shmid_ds.shm_perm.uid && uid != 0) 302 return EPERM; 303 if ((r = sys_datacopy(m->m_source, buf, SELF, 304 (vir_bytes)&tmp_ds, sizeof(tmp_ds))) != OK) 305 return r; 306 shm->shmid_ds.shm_perm.uid = tmp_ds.shm_perm.uid; 307 shm->shmid_ds.shm_perm.gid = tmp_ds.shm_perm.gid; 308 shm->shmid_ds.shm_perm.mode &= ~ACCESSPERMS; 309 shm->shmid_ds.shm_perm.mode |= 310 tmp_ds.shm_perm.mode & ACCESSPERMS; 311 shm->shmid_ds.shm_ctime = clock_time(NULL); 312 break; 313 case IPC_RMID: 314 uid = getnuid(m->m_source); 315 if (uid != shm->shmid_ds.shm_perm.cuid && 316 uid != shm->shmid_ds.shm_perm.uid && uid != 0) 317 return EPERM; 318 shm->shmid_ds.shm_perm.mode |= SHM_DEST; 319 /* Destroy if possible. */ 320 update_refcount_and_destroy(); 321 break; 322 case IPC_INFO: 323 memset(&sinfo, 0, sizeof(sinfo)); 324 sinfo.shmmax = (unsigned long) -1; 325 sinfo.shmmin = 1; 326 sinfo.shmmni = __arraycount(shm_list); 327 sinfo.shmseg = (unsigned long) -1; 328 sinfo.shmall = (unsigned long) -1; 329 if ((r = sys_datacopy(SELF, (vir_bytes)&sinfo, m->m_source, 330 buf, sizeof(sinfo))) != OK) 331 return r; 332 if (shm_list_nr > 0) 333 m->m_lc_ipc_shmctl.ret = shm_list_nr - 1; 334 else 335 m->m_lc_ipc_shmctl.ret = 0; 336 break; 337 case SHM_INFO: 338 memset(&s_info, 0, sizeof(s_info)); 339 s_info.used_ids = shm_list_nr; 340 s_info.shm_tot = 0; 341 for (i = 0; i < shm_list_nr; i++) 342 s_info.shm_tot += 343 shm_list[i].shmid_ds.shm_segsz / PAGE_SIZE; 344 s_info.shm_rss = s_info.shm_tot; 345 s_info.shm_swp = 0; 346 s_info.swap_attempts = 0; 347 s_info.swap_successes = 0; 348 if ((r = sys_datacopy(SELF, (vir_bytes)&s_info, m->m_source, 349 buf, sizeof(s_info))) != OK) 350 return r; 351 if (shm_list_nr > 0) 352 m->m_lc_ipc_shmctl.ret = shm_list_nr - 1; 353 else 354 m->m_lc_ipc_shmctl.ret = 0; 355 break; 356 default: 357 return EINVAL; 358 } 359 return OK; 360 } 361 362 #if 0 363 static void 364 list_shm_ds(void) 365 { 366 unsigned int i; 367 368 printf("key\tid\tpage\n"); 369 for (i = 0; i < shm_list_nr; i++) { 370 if (!(shm_list[i].shmid_ds.shm_perm.mode & SHM_ALLOC)) 371 continue; 372 printf("%ld\t%d\t%lx\n", 373 shm_list[i].shmid_ds.shm_perm._key, 374 IXSEQ_TO_IPCID(i, shm_list[i].shmid_ds.shm_perm), 375 shm_list[i].page); 376 } 377 } 378 #endif 379 380 int 381 is_shm_nil(void) 382 { 383 384 return (shm_list_nr == 0); 385 } 386