xref: /illumos-gate/usr/src/lib/libproc/common/pr_mmap.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/mman.h>
31 #include "libproc.h"
32 
33 /*
34  * mmap() system call -- executed by subject process
35  */
36 void *
37 pr_mmap(struct ps_prochandle *Pr,
38 	void *addr, size_t len, int prot, int flags, int fd, off_t off)
39 {
40 	sysret_t rval;			/* return value from mmap() */
41 	argdes_t argd[6];		/* arg descriptors for mmap() */
42 	argdes_t *adp;
43 	int error;
44 
45 	if (Pr == NULL)		/* no subject process */
46 		return (mmap(addr, len, prot, flags, fd, off));
47 
48 	adp = &argd[0];		/* addr argument */
49 	adp->arg_value = (long)addr;
50 	adp->arg_object = NULL;
51 	adp->arg_type = AT_BYVAL;
52 	adp->arg_inout = AI_INPUT;
53 	adp->arg_size = 0;
54 
55 	adp++;			/* len argument */
56 	adp->arg_value = (long)len;
57 	adp->arg_object = NULL;
58 	adp->arg_type = AT_BYVAL;
59 	adp->arg_inout = AI_INPUT;
60 	adp->arg_size = 0;
61 
62 	adp++;			/* prot argument */
63 	adp->arg_value = (long)prot;
64 	adp->arg_object = NULL;
65 	adp->arg_type = AT_BYVAL;
66 	adp->arg_inout = AI_INPUT;
67 	adp->arg_size = 0;
68 
69 	adp++;			/* flags argument */
70 	adp->arg_value = (long)(_MAP_NEW|flags);
71 	adp->arg_object = NULL;
72 	adp->arg_type = AT_BYVAL;
73 	adp->arg_inout = AI_INPUT;
74 	adp->arg_size = 0;
75 
76 	adp++;			/* fd argument */
77 	adp->arg_value = (long)fd;
78 	adp->arg_object = NULL;
79 	adp->arg_type = AT_BYVAL;
80 	adp->arg_inout = AI_INPUT;
81 	adp->arg_size = 0;
82 
83 	adp++;			/* off argument */
84 	adp->arg_value = (long)off;
85 	adp->arg_object = NULL;
86 	adp->arg_type = AT_BYVAL;
87 	adp->arg_inout = AI_INPUT;
88 	adp->arg_size = 0;
89 
90 	error = Psyscall(Pr, &rval, SYS_mmap, 6, &argd[0]);
91 
92 	if (error) {
93 		errno = (error > 0)? error : ENOSYS;
94 		return ((void *)(-1));
95 	}
96 	return ((void *)rval.sys_rval1);
97 }
98 
99 /*
100  * munmap() system call -- executed by subject process
101  */
102 int
103 pr_munmap(struct ps_prochandle *Pr, void *addr, size_t len)
104 {
105 	sysret_t rval;			/* return value from munmap() */
106 	argdes_t argd[2];		/* arg descriptors for munmap() */
107 	argdes_t *adp;
108 	int error;
109 
110 	if (Pr == NULL)		/* no subject process */
111 		return (munmap(addr, len));
112 
113 	adp = &argd[0];		/* addr argument */
114 	adp->arg_value = (long)addr;
115 	adp->arg_object = NULL;
116 	adp->arg_type = AT_BYVAL;
117 	adp->arg_inout = AI_INPUT;
118 	adp->arg_size = 0;
119 
120 	adp++;			/* len argument */
121 	adp->arg_value = (long)len;
122 	adp->arg_object = NULL;
123 	adp->arg_type = AT_BYVAL;
124 	adp->arg_inout = AI_INPUT;
125 	adp->arg_size = 0;
126 
127 	error = Psyscall(Pr, &rval, SYS_munmap, 2, &argd[0]);
128 
129 	if (error) {
130 		errno = (error > 0)? error : ENOSYS;
131 		return (-1);
132 	}
133 	return (rval.sys_rval1);
134 }
135 
136 /*
137  * zmap() -- convenience function; mmap(MAP_ANON) executed by subject process.
138  */
139 void *
140 pr_zmap(struct ps_prochandle *Pr, void *addr, size_t len, int prot, int flags)
141 {
142 	return (pr_mmap(Pr, addr, len, prot, flags | MAP_ANON, -1, (off_t)0));
143 }
144