1*7500Sroot /* subr_xxx.c 4.13 82/07/24 */ 234Sbill 334Sbill #include "../h/param.h" 434Sbill #include "../h/systm.h" 534Sbill #include "../h/conf.h" 634Sbill #include "../h/inode.h" 734Sbill #include "../h/dir.h" 834Sbill #include "../h/user.h" 934Sbill #include "../h/buf.h" 1034Sbill #include "../h/proc.h" 116573Smckusic #include "../h/fs.h" 127427Sroot #include "../h/vm.h" 137427Sroot #include "../h/pte.h" 147427Sroot #include "../h/cmap.h" 1534Sbill 1634Sbill /* 1734Sbill * Pass back c to the user at his location u_base; 1834Sbill * update u_base, u_count, and u_offset. Return -1 1934Sbill * on the last character of the user's read. 2034Sbill * u_base is in the user address space unless u_segflg is set. 2134Sbill */ 2234Sbill passc(c) 2334Sbill register c; 2434Sbill { 2534Sbill register id; 2634Sbill 276573Smckusic if ((id = u.u_segflg) == 1) 2834Sbill *u.u_base = c; 2934Sbill else 306573Smckusic if (id?suibyte(u.u_base, c):subyte(u.u_base, c) < 0) { 3134Sbill u.u_error = EFAULT; 326573Smckusic return (-1); 3334Sbill } 3434Sbill u.u_count--; 3534Sbill u.u_offset++; 3634Sbill u.u_base++; 376573Smckusic return (u.u_count == 0? -1: 0); 3834Sbill } 3934Sbill 403218Swnj #include "ct.h" 414972Swnj #if NCT > 0 4234Sbill /* 4334Sbill * Pick up and return the next character from the user's 4434Sbill * write call at location u_base; 4534Sbill * update u_base, u_count, and u_offset. Return -1 4634Sbill * when u_count is exhausted. u_base is in the user's 4734Sbill * address space unless u_segflg is set. 4834Sbill */ 4934Sbill cpass() 5034Sbill { 5134Sbill register c, id; 5234Sbill 536573Smckusic if (u.u_count == 0) 546573Smckusic return (-1); 556573Smckusic if ((id = u.u_segflg) == 1) 5634Sbill c = *u.u_base; 5734Sbill else 586573Smckusic if ((c = id==0?fubyte(u.u_base):fuibyte(u.u_base)) < 0) { 5934Sbill u.u_error = EFAULT; 606573Smckusic return (-1); 6134Sbill } 6234Sbill u.u_count--; 6334Sbill u.u_offset++; 6434Sbill u.u_base++; 656573Smckusic return (c&0377); 6634Sbill } 674972Swnj #endif 6834Sbill 6934Sbill /* 7034Sbill * Routine which sets a user error; placed in 7134Sbill * illegal entries in the bdevsw and cdevsw tables. 7234Sbill */ 7334Sbill nodev() 7434Sbill { 7534Sbill 7634Sbill u.u_error = ENODEV; 7734Sbill } 7834Sbill 7934Sbill /* 8034Sbill * Null routine; placed in insignificant entries 8134Sbill * in the bdevsw and cdevsw tables. 8234Sbill */ 8334Sbill nulldev() 8434Sbill { 8534Sbill 8634Sbill } 8734Sbill 8834Sbill imin(a, b) 8934Sbill { 9034Sbill 9134Sbill return (a < b ? a : b); 9234Sbill } 9334Sbill 9434Sbill imax(a, b) 9534Sbill { 9634Sbill 9734Sbill return (a > b ? a : b); 9834Sbill } 9934Sbill 1006573Smckusic unsigned 1016573Smckusic min(a, b) 1026573Smckusic unsigned int a, b; 1036573Smckusic { 1046573Smckusic 1056573Smckusic return (a < b ? a : b); 1066573Smckusic } 1076573Smckusic 1086573Smckusic unsigned 1096573Smckusic max(a, b) 1106573Smckusic unsigned int a, b; 1116573Smckusic { 1126573Smckusic 1136573Smckusic return (a > b ? a : b); 1146573Smckusic } 1156573Smckusic 11634Sbill struct proc * 11734Sbill pfind(pid) 11834Sbill int pid; 11934Sbill { 12034Sbill register struct proc *p; 12134Sbill 12234Sbill for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash]) 12334Sbill if (p->p_pid == pid) 12434Sbill return (p); 12534Sbill return ((struct proc *)0); 12634Sbill } 1277427Sroot extern cabase, calimit; 1287427Sroot extern struct pte camap[]; 1297427Sroot 1307427Sroot caddr_t cacur = (caddr_t)&cabase; 1317427Sroot caddr_t camax = (caddr_t)&cabase; 1327427Sroot int cax = 0; 1337427Sroot /* 1347427Sroot * This is a kernel-mode storage allocator. 1357427Sroot * It is very primitive, currently, in that 1367427Sroot * there is no way to give space back. 1377427Sroot * It serves, for the time being, the needs of 1387427Sroot * auto-configuration code and the like which 1397427Sroot * need to allocate some stuff at boot time. 1407427Sroot */ 1417427Sroot caddr_t 1427427Sroot calloc(size) 1437427Sroot int size; 1447427Sroot { 1457427Sroot register caddr_t res; 1467427Sroot register int i; 1477427Sroot 1487427Sroot if (cacur+size >= (caddr_t)&calimit) 1497427Sroot panic("calloc"); 1507427Sroot while (cacur+size > camax) { 1517427Sroot (void) vmemall(&camap[cax], CLSIZE, &proc[0], CSYS); 1527427Sroot vmaccess(&camap[cax], camax, CLSIZE); 1537427Sroot for (i = 0; i < CLSIZE; i++) 1547427Sroot clearseg(camap[cax++].pg_pfnum); 1557427Sroot camax += NBPG * CLSIZE; 1567427Sroot } 1577427Sroot res = cacur; 1587427Sroot cacur += size; 1597427Sroot return (res); 1607427Sroot } 1617427Sroot #ifndef vax 1627427Sroot ffs(mask) 1637427Sroot register long mask; 1647427Sroot { 1657427Sroot register int i; 1667427Sroot 1677427Sroot for(i=1; i<NSIG; i++) { 1687427Sroot if (mask & 1) 1697427Sroot return (i); 1707427Sroot mask >>= 1; 1717427Sroot } 1727427Sroot return (0); 1737427Sroot } 1747427Sroot #endif 175*7500Sroot #ifndef vax 176*7500Sroot ffs(mask) 177*7500Sroot register long mask; 178*7500Sroot { 179*7500Sroot register int i; 180*7500Sroot 181*7500Sroot for(i=1; i<NSIG; i++) { 182*7500Sroot if (mask & 1) 183*7500Sroot return (i); 184*7500Sroot mask >>= 1; 185*7500Sroot } 186*7500Sroot return (0); 187*7500Sroot } 188*7500Sroot #endif 189*7500Sroot 190