4 * Implementation of SVID semaphores
6 * Author: Daniel Boulet
8 * This software is provided ``AS IS'' without any warranties of any kind.
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/sysproto.h>
14 #include <sys/kernel.h>
17 #include <sys/sysent.h>
19 static void seminit __P((void *));
20 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
22 #ifndef _SYS_SYSPROTO_H_
24 int __semctl __P((struct proc *p, struct __semctl_args *uap));
26 int semget __P((struct proc *p, struct semget_args *uap));
28 int semop __P((struct proc *p, struct semop_args *uap));
29 struct semconfig_args;
30 int semconfig __P((struct proc *p, struct semconfig_args *uap));
33 static struct sem_undo *semu_alloc __P((struct proc *p));
34 static int semundo_adjust __P((struct proc *p, struct sem_undo **supptr,
35 int semid, int semnum, int adjval));
36 static void semundo_clear __P((int semid, int semnum));
38 /* XXX casting to (sy_call_t *) is bogus, as usual. */
39 static sy_call_t *semcalls[] = {
40 (sy_call_t *)__semctl, (sy_call_t *)semget,
41 (sy_call_t *)semop, (sy_call_t *)semconfig
44 static int semtot = 0;
45 struct semid_ds *sema; /* semaphore id pool */
46 struct sem *sem; /* semaphore pool */
47 static struct sem_undo *semu_list; /* list of active undo structures */
48 int *semu; /* undo structure pool */
50 static struct proc *semlock_holder = NULL;
59 panic("sema is NULL");
61 panic("semu is NULL");
63 for (i = 0; i < seminfo.semmni; i++) {
65 sema[i].sem_perm.mode = 0;
67 for (i = 0; i < seminfo.semmnu; i++) {
68 register struct sem_undo *suptr = SEMU(i);
69 suptr->un_proc = NULL;
75 * Entry point for all SEM calls
80 /* XXX actually varargs. */
81 struct semsys_args /* {
90 while (semlock_holder != NULL && semlock_holder != p)
91 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semsys", 0);
93 if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
95 return ((*semcalls[uap->which])(p, &uap->a2));
99 * Lock or unlock the entire semaphore facility.
101 * This will probably eventually evolve into a general purpose semaphore
102 * facility status enquiry mechanism (I don't like the "read /dev/kmem"
103 * approach currently taken by ipcs and the amount of info that we want
104 * to be able to extract for ipcs is probably beyond what the capability
105 * of the getkerninfo facility.
107 * At the time that the current version of semconfig was written, ipcs is
108 * the only user of the semconfig facility. It uses it to ensure that the
109 * semaphore facility data structures remain static while it fishes around
113 #ifndef _SYS_SYSPROTO_H_
114 struct semconfig_args {
115 semconfig_ctl_t flag;
122 struct semconfig_args *uap;
127 case SEM_CONFIG_FREEZE:
131 case SEM_CONFIG_THAW:
132 semlock_holder = NULL;
133 wakeup((caddr_t)&semlock_holder);
137 printf("semconfig: unknown flag parameter value (%d) - ignored\n",
148 * Allocate a new sem_undo structure for a process
149 * (returns ptr to structure or NULL if no more room)
152 static struct sem_undo *
157 register struct sem_undo *suptr;
158 register struct sem_undo **supptr;
162 * Try twice to allocate something.
163 * (we'll purge any empty structures after the first pass so
164 * two passes are always enough)
167 for (attempt = 0; attempt < 2; attempt++) {
169 * Look for a free structure.
170 * Fill it in and return it if we find one.
173 for (i = 0; i < seminfo.semmnu; i++) {
175 if (suptr->un_proc == NULL) {
176 suptr->un_next = semu_list;
185 * We didn't find a free one, if this is the first attempt
186 * then try to free some structures.
190 /* All the structures are in use - try to free some */
191 int did_something = 0;
194 while ((suptr = *supptr) != NULL) {
195 if (suptr->un_cnt == 0) {
196 suptr->un_proc = NULL;
197 *supptr = suptr->un_next;
200 supptr = &(suptr->un_next);
203 /* If we didn't free anything then just give-up */
208 * The second pass failed even though we freed
209 * something after the first pass!
210 * This is IMPOSSIBLE!
212 panic("semu_alloc - second attempt failed");
219 * Adjust a particular entry for a particular proc
223 semundo_adjust(p, supptr, semid, semnum, adjval)
224 register struct proc *p;
225 struct sem_undo **supptr;
229 register struct sem_undo *suptr;
230 register struct undo *sunptr;
233 /* Look for and remember the sem_undo if the caller doesn't provide
238 for (suptr = semu_list; suptr != NULL;
239 suptr = suptr->un_next) {
240 if (suptr->un_proc == p) {
248 suptr = semu_alloc(p);
256 * Look for the requested entry and adjust it (delete if adjval becomes
259 sunptr = &suptr->un_ent[0];
260 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
261 if (sunptr->un_id != semid || sunptr->un_num != semnum)
264 sunptr->un_adjval = 0;
266 sunptr->un_adjval += adjval;
267 if (sunptr->un_adjval == 0) {
269 if (i < suptr->un_cnt)
271 suptr->un_ent[suptr->un_cnt];
276 /* Didn't find the right entry - create it */
279 if (suptr->un_cnt != seminfo.semume) {
280 sunptr = &suptr->un_ent[suptr->un_cnt];
282 sunptr->un_adjval = adjval;
283 sunptr->un_id = semid; sunptr->un_num = semnum;
290 semundo_clear(semid, semnum)
293 register struct sem_undo *suptr;
295 for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
296 register struct undo *sunptr = &suptr->un_ent[0];
299 while (i < suptr->un_cnt) {
300 if (sunptr->un_id == semid) {
301 if (semnum == -1 || sunptr->un_num == semnum) {
303 if (i < suptr->un_cnt) {
305 suptr->un_ent[suptr->un_cnt];
318 * Note that the user-mode half of this passes a union, not a pointer
320 #ifndef _SYS_SYSPROTO_H_
321 struct __semctl_args {
332 register struct __semctl_args *uap;
334 int semid = uap->semid;
335 int semnum = uap->semnum;
337 union semun *arg = uap->arg;
338 union semun real_arg;
339 struct ucred *cred = p->p_ucred;
341 struct semid_ds sbuf;
342 register struct semid_ds *semaptr;
345 printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
348 semid = IPCID_TO_IX(semid);
349 if (semid < 0 || semid >= seminfo.semmsl)
352 semaptr = &sema[semid];
353 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
354 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
362 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
364 semaptr->sem_perm.cuid = cred->cr_uid;
365 semaptr->sem_perm.uid = cred->cr_uid;
366 semtot -= semaptr->sem_nsems;
367 for (i = semaptr->sem_base - sem; i < semtot; i++)
368 sem[i] = sem[i + semaptr->sem_nsems];
369 for (i = 0; i < seminfo.semmni; i++) {
370 if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
371 sema[i].sem_base > semaptr->sem_base)
372 sema[i].sem_base -= semaptr->sem_nsems;
374 semaptr->sem_perm.mode = 0;
375 semundo_clear(semid, -1);
376 wakeup((caddr_t)semaptr);
380 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
382 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
384 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
387 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
388 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
389 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
390 (sbuf.sem_perm.mode & 0777);
391 semaptr->sem_ctime = time_second;
395 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
397 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
399 eval = copyout((caddr_t)semaptr, real_arg.buf,
400 sizeof(struct semid_ds));
404 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
406 if (semnum < 0 || semnum >= semaptr->sem_nsems)
408 rval = semaptr->sem_base[semnum].semncnt;
412 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
414 if (semnum < 0 || semnum >= semaptr->sem_nsems)
416 rval = semaptr->sem_base[semnum].sempid;
420 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
422 if (semnum < 0 || semnum >= semaptr->sem_nsems)
424 rval = semaptr->sem_base[semnum].semval;
428 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
430 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
432 for (i = 0; i < semaptr->sem_nsems; i++) {
433 eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
434 &real_arg.array[i], sizeof(real_arg.array[0]));
441 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
443 if (semnum < 0 || semnum >= semaptr->sem_nsems)
445 rval = semaptr->sem_base[semnum].semzcnt;
449 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
451 if (semnum < 0 || semnum >= semaptr->sem_nsems)
453 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
455 semaptr->sem_base[semnum].semval = real_arg.val;
456 semundo_clear(semid, semnum);
457 wakeup((caddr_t)semaptr);
461 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
463 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
465 for (i = 0; i < semaptr->sem_nsems; i++) {
466 eval = copyin(&real_arg.array[i],
467 (caddr_t)&semaptr->sem_base[i].semval,
468 sizeof(real_arg.array[0]));
472 semundo_clear(semid, -1);
473 wakeup((caddr_t)semaptr);
481 p->p_retval[0] = rval;
485 #ifndef _SYS_SYSPROTO_H_
496 register struct semget_args *uap;
500 int nsems = uap->nsems;
501 int semflg = uap->semflg;
502 struct ucred *cred = p->p_ucred;
505 printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
508 if (key != IPC_PRIVATE) {
509 for (semid = 0; semid < seminfo.semmni; semid++) {
510 if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
511 sema[semid].sem_perm.key == key)
514 if (semid < seminfo.semmni) {
516 printf("found public key\n");
518 if ((eval = ipcperm(p, &sema[semid].sem_perm,
521 if (nsems > 0 && sema[semid].sem_nsems < nsems) {
523 printf("too small\n");
527 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
529 printf("not exclusive\n");
538 printf("need to allocate the semid_ds\n");
540 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
541 if (nsems <= 0 || nsems > seminfo.semmsl) {
543 printf("nsems out of range (0<%d<=%d)\n", nsems,
548 if (nsems > seminfo.semmns - semtot) {
550 printf("not enough semaphores left (need %d, got %d)\n",
551 nsems, seminfo.semmns - semtot);
555 for (semid = 0; semid < seminfo.semmni; semid++) {
556 if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
559 if (semid == seminfo.semmni) {
561 printf("no more semid_ds's available\n");
566 printf("semid %d is available\n", semid);
568 sema[semid].sem_perm.key = key;
569 sema[semid].sem_perm.cuid = cred->cr_uid;
570 sema[semid].sem_perm.uid = cred->cr_uid;
571 sema[semid].sem_perm.cgid = cred->cr_gid;
572 sema[semid].sem_perm.gid = cred->cr_gid;
573 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
574 sema[semid].sem_perm.seq =
575 (sema[semid].sem_perm.seq + 1) & 0x7fff;
576 sema[semid].sem_nsems = nsems;
577 sema[semid].sem_otime = 0;
578 sema[semid].sem_ctime = time_second;
579 sema[semid].sem_base = &sem[semtot];
581 bzero(sema[semid].sem_base,
582 sizeof(sema[semid].sem_base[0])*nsems);
584 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
589 printf("didn't find it and wasn't asked to create it\n");
595 p->p_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
599 #ifndef _SYS_SYSPROTO_H_
610 register struct semop_args *uap;
612 int semid = uap->semid;
613 int nsops = uap->nsops;
614 struct sembuf sops[MAX_SOPS];
615 register struct semid_ds *semaptr;
616 register struct sembuf *sopptr;
617 register struct sem *semptr;
618 struct sem_undo *suptr = NULL;
620 int do_wakeup, do_undos;
623 printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
626 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
628 if (semid < 0 || semid >= seminfo.semmsl)
631 semaptr = &sema[semid];
632 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
634 if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
637 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W))) {
639 printf("eval = %d from ipaccess\n", eval);
644 if (nsops > MAX_SOPS) {
646 printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
651 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
653 printf("eval = %d from copyin(%08x, %08x, %d)\n", eval,
654 uap->sops, &sops, nsops * sizeof(sops[0]));
660 * Loop trying to satisfy the vector of requests.
661 * If we reach a point where we must wait, any requests already
662 * performed are rolled back and we go to sleep until some other
663 * process wakes us up. At this point, we start all over again.
665 * This ensures that from the perspective of other tasks, a set
666 * of requests is atomic (never partially satisfied).
673 for (i = 0; i < nsops; i++) {
676 if (sopptr->sem_num >= semaptr->sem_nsems)
679 semptr = &semaptr->sem_base[sopptr->sem_num];
682 printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
683 semaptr, semaptr->sem_base, semptr,
684 sopptr->sem_num, semptr->semval, sopptr->sem_op,
685 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
688 if (sopptr->sem_op < 0) {
689 if (semptr->semval + sopptr->sem_op < 0) {
691 printf("semop: can't do it now\n");
695 semptr->semval += sopptr->sem_op;
696 if (semptr->semval == 0 &&
700 if (sopptr->sem_flg & SEM_UNDO)
702 } else if (sopptr->sem_op == 0) {
703 if (semptr->semval > 0) {
705 printf("semop: not zero now\n");
710 if (semptr->semncnt > 0)
712 semptr->semval += sopptr->sem_op;
713 if (sopptr->sem_flg & SEM_UNDO)
719 * Did we get through the entire vector?
725 * No ... rollback anything that we've already done
728 printf("semop: rollback 0 through %d\n", i-1);
730 for (j = 0; j < i; j++)
731 semaptr->sem_base[sops[j].sem_num].semval -=
735 * If the request that we couldn't satisfy has the
736 * NOWAIT flag set then return with EAGAIN.
738 if (sopptr->sem_flg & IPC_NOWAIT)
741 if (sopptr->sem_op == 0)
747 printf("semop: good night!\n");
749 eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
752 printf("semop: good morning (eval=%d)!\n", eval);
755 suptr = NULL; /* sem_undo may have been reallocated */
760 printf("semop: good morning!\n");
764 * Make sure that the semaphore still exists
766 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
767 semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
768 /* The man page says to return EIDRM. */
769 /* Unfortunately, BSD doesn't define that code! */
778 * The semaphore is still alive. Readjust the count of
781 if (sopptr->sem_op == 0)
789 * Process any SEM_UNDO requests.
792 for (i = 0; i < nsops; i++) {
794 * We only need to deal with SEM_UNDO's for non-zero
799 if ((sops[i].sem_flg & SEM_UNDO) == 0)
801 adjval = sops[i].sem_op;
804 eval = semundo_adjust(p, &suptr, semid,
805 sops[i].sem_num, -adjval);
810 * Oh-Oh! We ran out of either sem_undo's or undo's.
811 * Rollback the adjustments to this point and then
812 * rollback the semaphore ups and down so we can return
813 * with an error with all structures restored. We
814 * rollback the undo's in the exact reverse order that
815 * we applied them. This guarantees that we won't run
816 * out of space as we roll things back out.
818 for (j = i - 1; j >= 0; j--) {
819 if ((sops[j].sem_flg & SEM_UNDO) == 0)
821 adjval = sops[j].sem_op;
824 if (semundo_adjust(p, &suptr, semid,
825 sops[j].sem_num, adjval) != 0)
826 panic("semop - can't undo undos");
829 for (j = 0; j < nsops; j++)
830 semaptr->sem_base[sops[j].sem_num].semval -=
834 printf("eval = %d from semundo_adjust\n", eval);
837 } /* loop through the sops */
838 } /* if (do_undos) */
840 /* We're definitely done - set the sempid's */
841 for (i = 0; i < nsops; i++) {
843 semptr = &semaptr->sem_base[sopptr->sem_num];
844 semptr->sempid = p->p_pid;
847 /* Do a wakeup if any semaphore was up'd. */
850 printf("semop: doing wakeup\n");
852 wakeup((caddr_t)semaptr);
854 printf("semop: back from wakeup\n");
858 printf("semop: done\n");
865 * Go through the undo structures for this process and apply the adjustments to
872 register struct sem_undo *suptr;
873 register struct sem_undo **supptr;
877 * If somebody else is holding the global semaphore facility lock
878 * then sleep until it is released.
880 while (semlock_holder != NULL && semlock_holder != p) {
882 printf("semaphore facility locked - sleeping ...\n");
884 (void) tsleep((caddr_t)&semlock_holder, (PZERO - 4), "semext", 0);
890 * Go through the chain of undo vectors looking for one
891 * associated with this process.
894 for (supptr = &semu_list; (suptr = *supptr) != NULL;
895 supptr = &suptr->un_next) {
896 if (suptr->un_proc == p)
904 printf("proc @%08x has undo structure with %d entries\n", p,
909 * If there are any active undo elements then process them.
911 if (suptr->un_cnt > 0) {
914 for (ix = 0; ix < suptr->un_cnt; ix++) {
915 int semid = suptr->un_ent[ix].un_id;
916 int semnum = suptr->un_ent[ix].un_num;
917 int adjval = suptr->un_ent[ix].un_adjval;
918 struct semid_ds *semaptr;
920 semaptr = &sema[semid];
921 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
922 panic("semexit - semid not allocated");
923 if (semnum >= semaptr->sem_nsems)
924 panic("semexit - semnum out of range");
927 printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
928 suptr->un_proc, suptr->un_ent[ix].un_id,
929 suptr->un_ent[ix].un_num,
930 suptr->un_ent[ix].un_adjval,
931 semaptr->sem_base[semnum].semval);
935 if (semaptr->sem_base[semnum].semval < -adjval)
936 semaptr->sem_base[semnum].semval = 0;
938 semaptr->sem_base[semnum].semval +=
941 semaptr->sem_base[semnum].semval += adjval;
943 wakeup((caddr_t)semaptr);
945 printf("semexit: back from wakeup\n");
951 * Deallocate the undo vector.
954 printf("removing vector\n");
956 suptr->un_proc = NULL;
957 *supptr = suptr->un_next;
961 * If the exiting process is holding the global semaphore facility
962 * lock then release it.
964 if (semlock_holder == p) {
965 semlock_holder = NULL;
966 wakeup((caddr_t)&semlock_holder);