xref: /titanic_44/usr/src/lib/libtnfctl/prb_shmem.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Interfaces to allocate, control, and free a shared memory lock
30*7c478bd9Sstevel@tonic-gate  * XXXX Could we use a semaphore or a shared memory condition variable
31*7c478bd9Sstevel@tonic-gate  * instead ?
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "prb_proc_int.h"
42*7c478bd9Sstevel@tonic-gate #include "dbg.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static boolean_t getspin(volatile shmem_msg_t *smp);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * prb_shmem_init() - Allocates and initializes the shared memory region
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate prb_status_t
prb_shmem_init(volatile shmem_msg_t ** ret_val)50*7c478bd9Sstevel@tonic-gate prb_shmem_init(volatile shmem_msg_t **ret_val)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	int		shmem_fd;
53*7c478bd9Sstevel@tonic-gate 	volatile	shmem_msg_t *smp;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_shmem_init_1, "libtnfctl", "sunw%verbosity 2");
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	shmem_fd = open("/dev/zero", O_RDWR);
58*7c478bd9Sstevel@tonic-gate 	if (shmem_fd == -1) {
59*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr, "couldn't open \"/dev/zero\""));
60*7c478bd9Sstevel@tonic-gate 		return (prb_status_map(errno));
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
63*7c478bd9Sstevel@tonic-gate 	smp = (shmem_msg_t *) mmap(0, sizeof (struct shmem_msg),
64*7c478bd9Sstevel@tonic-gate 			PROT_READ | PROT_WRITE, MAP_SHARED,
65*7c478bd9Sstevel@tonic-gate 			shmem_fd, 0);
66*7c478bd9Sstevel@tonic-gate 	if (smp == (struct shmem_msg *) - 1) {
67*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr, "couldn't mmap \"/dev/zero\""));
68*7c478bd9Sstevel@tonic-gate 		return (prb_status_map(errno));
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 	(void) close(shmem_fd);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/* sets the shared memory region to cause waiting */
73*7c478bd9Sstevel@tonic-gate 	smp->spin = B_TRUE;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	*ret_val = smp;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  * prb_shmem_wait() - spins until the shared memory flag is cleared
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate static boolean_t
getspin(volatile shmem_msg_t * smp)84*7c478bd9Sstevel@tonic-gate getspin(volatile shmem_msg_t *smp)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	return (smp->spin);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate prb_status_t
prb_shmem_wait(volatile shmem_msg_t * smp)90*7c478bd9Sstevel@tonic-gate prb_shmem_wait(volatile shmem_msg_t *smp)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_shmem_wait_start, "libtnfctl",
93*7c478bd9Sstevel@tonic-gate 		"sunw%verbosity 2; start prb_shmem_wait");
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	while (getspin(smp));
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_shmem_wait_end, "libtnfctl",
98*7c478bd9Sstevel@tonic-gate 		"sunw%verbosity 2; end prb_shmem_wait");
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * prb_shmem_clear() - clears the shared memory flag and allows waiters to
107*7c478bd9Sstevel@tonic-gate  * proceed.
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate prb_status_t
prb_shmem_clear(volatile shmem_msg_t * smp)110*7c478bd9Sstevel@tonic-gate prb_shmem_clear(volatile shmem_msg_t *smp)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_shmem_clear_1, "libtnfctl", "sunw%verbosity 2");
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	smp->spin = B_FALSE;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * prb_shmem_free() - Unmaps the shared memory region.
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate prb_status_t
prb_shmem_free(volatile shmem_msg_t * smp)123*7c478bd9Sstevel@tonic-gate prb_shmem_free(volatile shmem_msg_t *smp)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	DBG_TNF_PROBE_0(prb_shmem_free_1, "libtnfctl", "sunw%verbosity 2");
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (munmap((caddr_t) smp, sizeof (struct shmem_msg)) != 0) {
128*7c478bd9Sstevel@tonic-gate 		DBG((void) fprintf(stderr, "couldn't munmap shared memory\n"));
129*7c478bd9Sstevel@tonic-gate 		return (prb_status_map(errno));
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	return (PRB_STATUS_OK);
133*7c478bd9Sstevel@tonic-gate }
134