xref: /titanic_44/usr/src/lib/libproc/common/pr_mmap.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/mman.h>
33 #include "libproc.h"
34 
35 /*
36  * mmap() system call -- executed by subject process
37  */
38 void *
39 pr_mmap(struct ps_prochandle *Pr,
40 	void *addr, size_t len, int prot, int flags, int fd, off_t off)
41 {
42 	sysret_t rval;			/* return value from mmap() */
43 	argdes_t argd[6];		/* arg descriptors for mmap() */
44 	argdes_t *adp;
45 	int error;
46 
47 	if (Pr == NULL)		/* no subject process */
48 		return (mmap(addr, len, prot, flags, fd, off));
49 
50 	adp = &argd[0];		/* addr argument */
51 	adp->arg_value = (long)addr;
52 	adp->arg_object = NULL;
53 	adp->arg_type = AT_BYVAL;
54 	adp->arg_inout = AI_INPUT;
55 	adp->arg_size = 0;
56 
57 	adp++;			/* len argument */
58 	adp->arg_value = (long)len;
59 	adp->arg_object = NULL;
60 	adp->arg_type = AT_BYVAL;
61 	adp->arg_inout = AI_INPUT;
62 	adp->arg_size = 0;
63 
64 	adp++;			/* prot argument */
65 	adp->arg_value = (long)prot;
66 	adp->arg_object = NULL;
67 	adp->arg_type = AT_BYVAL;
68 	adp->arg_inout = AI_INPUT;
69 	adp->arg_size = 0;
70 
71 	adp++;			/* flags argument */
72 	adp->arg_value = (long)(_MAP_NEW|flags);
73 	adp->arg_object = NULL;
74 	adp->arg_type = AT_BYVAL;
75 	adp->arg_inout = AI_INPUT;
76 	adp->arg_size = 0;
77 
78 	adp++;			/* fd argument */
79 	adp->arg_value = (long)fd;
80 	adp->arg_object = NULL;
81 	adp->arg_type = AT_BYVAL;
82 	adp->arg_inout = AI_INPUT;
83 	adp->arg_size = 0;
84 
85 	adp++;			/* off argument */
86 	adp->arg_value = (long)off;
87 	adp->arg_object = NULL;
88 	adp->arg_type = AT_BYVAL;
89 	adp->arg_inout = AI_INPUT;
90 	adp->arg_size = 0;
91 
92 	error = Psyscall(Pr, &rval, SYS_mmap, 6, &argd[0]);
93 
94 	if (error) {
95 		errno = (error > 0)? error : ENOSYS;
96 		return ((void *)(-1));
97 	}
98 	return ((void *)rval.sys_rval1);
99 }
100 
101 /*
102  * munmap() system call -- executed by subject process
103  */
104 int
105 pr_munmap(struct ps_prochandle *Pr, void *addr, size_t len)
106 {
107 	sysret_t rval;			/* return value from munmap() */
108 	argdes_t argd[2];		/* arg descriptors for munmap() */
109 	argdes_t *adp;
110 	int error;
111 
112 	if (Pr == NULL)		/* no subject process */
113 		return (munmap(addr, len));
114 
115 	adp = &argd[0];		/* addr argument */
116 	adp->arg_value = (long)addr;
117 	adp->arg_object = NULL;
118 	adp->arg_type = AT_BYVAL;
119 	adp->arg_inout = AI_INPUT;
120 	adp->arg_size = 0;
121 
122 	adp++;			/* len argument */
123 	adp->arg_value = (long)len;
124 	adp->arg_object = NULL;
125 	adp->arg_type = AT_BYVAL;
126 	adp->arg_inout = AI_INPUT;
127 	adp->arg_size = 0;
128 
129 	error = Psyscall(Pr, &rval, SYS_munmap, 2, &argd[0]);
130 
131 	if (error) {
132 		errno = (error > 0)? error : ENOSYS;
133 		return (-1);
134 	}
135 	return (rval.sys_rval1);
136 }
137 
138 /*
139  * zmap() -- convenience function; mmap(MAP_ANON) executed by subject process.
140  */
141 void *
142 pr_zmap(struct ps_prochandle *Pr, void *addr, size_t len, int prot, int flags)
143 {
144 	return (pr_mmap(Pr, addr, len, prot, flags | MAP_ANON, -1, (off_t)0));
145 }
146