xref: /minix3/minix/lib/libc/sys/mmap.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #define _SYSTEM 1
2*433d6423SLionel Sambuc #define _MINIX_SYSTEM 1
3*433d6423SLionel Sambuc #include <sys/cdefs.h>
4*433d6423SLionel Sambuc #include "namespace.h"
5*433d6423SLionel Sambuc #include <lib.h>
6*433d6423SLionel Sambuc #include <minix/u64.h>
7*433d6423SLionel Sambuc #include <minix/vm.h>
8*433d6423SLionel Sambuc 
9*433d6423SLionel Sambuc /* INCLUDES HERE */
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc #include <sys/mman.h>
12*433d6423SLionel Sambuc #include <stdarg.h>
13*433d6423SLionel Sambuc #include <string.h>
14*433d6423SLionel Sambuc #include <errno.h>
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc #ifdef __weak_alias
__weak_alias(mmap,_mmap)17*433d6423SLionel Sambuc __weak_alias(mmap, _mmap)
18*433d6423SLionel Sambuc __weak_alias(munmap, _munmap)
19*433d6423SLionel Sambuc #endif
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc void *minix_mmap_for(endpoint_t forwhom,
22*433d6423SLionel Sambuc 	void *addr, size_t len, int prot, int flags, int fd, off_t offset)
23*433d6423SLionel Sambuc {
24*433d6423SLionel Sambuc 	message m;
25*433d6423SLionel Sambuc 	int r;
26*433d6423SLionel Sambuc 
27*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
28*433d6423SLionel Sambuc 	m.m_mmap.addr = addr;
29*433d6423SLionel Sambuc 	m.m_mmap.len = len;
30*433d6423SLionel Sambuc 	m.m_mmap.prot = prot;
31*433d6423SLionel Sambuc 	m.m_mmap.flags = flags;
32*433d6423SLionel Sambuc 	m.m_mmap.fd = fd;
33*433d6423SLionel Sambuc 	m.m_mmap.offset = offset;
34*433d6423SLionel Sambuc 	m.m_mmap.forwhom = forwhom;
35*433d6423SLionel Sambuc 
36*433d6423SLionel Sambuc 	if(forwhom != SELF) {
37*433d6423SLionel Sambuc 		m.m_mmap.flags |= MAP_THIRDPARTY;
38*433d6423SLionel Sambuc 	}
39*433d6423SLionel Sambuc 
40*433d6423SLionel Sambuc 	r = _syscall(VM_PROC_NR, VM_MMAP, &m);
41*433d6423SLionel Sambuc 
42*433d6423SLionel Sambuc 	if(r != OK) {
43*433d6423SLionel Sambuc 		return MAP_FAILED;
44*433d6423SLionel Sambuc 	}
45*433d6423SLionel Sambuc 
46*433d6423SLionel Sambuc 	return m.m_mmap.retaddr;
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc 
minix_vfs_mmap(endpoint_t who,off_t offset,size_t len,dev_t dev,ino_t ino,int fd,u32_t vaddr,u16_t clearend,u16_t flags)49*433d6423SLionel Sambuc int minix_vfs_mmap(endpoint_t who, off_t offset, size_t len,
50*433d6423SLionel Sambuc 	dev_t dev, ino_t ino, int fd, u32_t vaddr, u16_t clearend,
51*433d6423SLionel Sambuc 	u16_t flags)
52*433d6423SLionel Sambuc {
53*433d6423SLionel Sambuc 	message m;
54*433d6423SLionel Sambuc 
55*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(message));
56*433d6423SLionel Sambuc 
57*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.who = who;
58*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.offset = offset;
59*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.dev = dev;
60*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.ino = ino;
61*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.vaddr = vaddr;
62*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.len = len;
63*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.fd = fd;
64*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.clearend = clearend;
65*433d6423SLionel Sambuc 	m.m_vm_vfs_mmap.flags = flags;
66*433d6423SLionel Sambuc 
67*433d6423SLionel Sambuc 	return _syscall(VM_PROC_NR, VM_VFS_MMAP, &m);
68*433d6423SLionel Sambuc }
69*433d6423SLionel Sambuc 
mmap(void * addr,size_t len,int prot,int flags,int fd,off_t offset)70*433d6423SLionel Sambuc void *mmap(void *addr, size_t len, int prot, int flags,
71*433d6423SLionel Sambuc 	int fd, off_t offset)
72*433d6423SLionel Sambuc {
73*433d6423SLionel Sambuc 	return minix_mmap_for(SELF, addr, len, prot, flags, fd, offset);
74*433d6423SLionel Sambuc }
75*433d6423SLionel Sambuc 
munmap(void * addr,size_t len)76*433d6423SLionel Sambuc int munmap(void *addr, size_t len)
77*433d6423SLionel Sambuc {
78*433d6423SLionel Sambuc 	message m;
79*433d6423SLionel Sambuc 
80*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
81*433d6423SLionel Sambuc 	m.VMUM_ADDR = addr;
82*433d6423SLionel Sambuc 	m.VMUM_LEN = len;
83*433d6423SLionel Sambuc 
84*433d6423SLionel Sambuc 	return _syscall(VM_PROC_NR, VM_MUNMAP, &m);
85*433d6423SLionel Sambuc }
86*433d6423SLionel Sambuc 
87*433d6423SLionel Sambuc 
vm_remap(endpoint_t d,endpoint_t s,void * da,void * sa,size_t size)88*433d6423SLionel Sambuc void *vm_remap(endpoint_t d,
89*433d6423SLionel Sambuc 			endpoint_t s,
90*433d6423SLionel Sambuc 			void *da,
91*433d6423SLionel Sambuc 			void *sa,
92*433d6423SLionel Sambuc 			size_t size)
93*433d6423SLionel Sambuc {
94*433d6423SLionel Sambuc 	message m;
95*433d6423SLionel Sambuc 	int r;
96*433d6423SLionel Sambuc 
97*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
98*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.destination = d;
99*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.source = s;
100*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.dest_addr = da;
101*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.src_addr = sa;
102*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.size = size;
103*433d6423SLionel Sambuc 
104*433d6423SLionel Sambuc 	r = _syscall(VM_PROC_NR, VM_REMAP, &m);
105*433d6423SLionel Sambuc 	if (r != OK)
106*433d6423SLionel Sambuc 		return MAP_FAILED;
107*433d6423SLionel Sambuc 	return m.m_lsys_vm_vmremap.ret_addr;
108*433d6423SLionel Sambuc }
109*433d6423SLionel Sambuc 
vm_remap_ro(endpoint_t d,endpoint_t s,void * da,void * sa,size_t size)110*433d6423SLionel Sambuc void *vm_remap_ro(endpoint_t d,
111*433d6423SLionel Sambuc 			endpoint_t s,
112*433d6423SLionel Sambuc 			void *da,
113*433d6423SLionel Sambuc 			void *sa,
114*433d6423SLionel Sambuc 			size_t size)
115*433d6423SLionel Sambuc {
116*433d6423SLionel Sambuc 	message m;
117*433d6423SLionel Sambuc 	int r;
118*433d6423SLionel Sambuc 
119*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
120*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.destination = d;
121*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.source = s;
122*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.dest_addr = da;
123*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.src_addr = sa;
124*433d6423SLionel Sambuc 	m.m_lsys_vm_vmremap.size = size;
125*433d6423SLionel Sambuc 
126*433d6423SLionel Sambuc 	r = _syscall(VM_PROC_NR, VM_REMAP_RO, &m);
127*433d6423SLionel Sambuc 	if (r != OK)
128*433d6423SLionel Sambuc 		return MAP_FAILED;
129*433d6423SLionel Sambuc 	return m.m_lsys_vm_vmremap.ret_addr;
130*433d6423SLionel Sambuc }
131*433d6423SLionel Sambuc 
vm_unmap(endpoint_t endpt,void * addr)132*433d6423SLionel Sambuc int vm_unmap(endpoint_t endpt, void *addr)
133*433d6423SLionel Sambuc {
134*433d6423SLionel Sambuc 	message m;
135*433d6423SLionel Sambuc 
136*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
137*433d6423SLionel Sambuc 	m.m_lc_vm_shm_unmap.forwhom = endpt;
138*433d6423SLionel Sambuc 	m.m_lc_vm_shm_unmap.addr = addr;
139*433d6423SLionel Sambuc 
140*433d6423SLionel Sambuc 	return _syscall(VM_PROC_NR, VM_SHM_UNMAP, &m);
141*433d6423SLionel Sambuc }
142*433d6423SLionel Sambuc 
vm_getphys(endpoint_t endpt,void * addr)143*433d6423SLionel Sambuc unsigned long vm_getphys(endpoint_t endpt, void *addr)
144*433d6423SLionel Sambuc {
145*433d6423SLionel Sambuc 	message m;
146*433d6423SLionel Sambuc 	int r;
147*433d6423SLionel Sambuc 
148*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
149*433d6423SLionel Sambuc 	m.m_lc_vm_getphys.endpt = endpt;
150*433d6423SLionel Sambuc 	m.m_lc_vm_getphys.addr = addr;
151*433d6423SLionel Sambuc 
152*433d6423SLionel Sambuc 	r = _syscall(VM_PROC_NR, VM_GETPHYS, &m);
153*433d6423SLionel Sambuc 	if (r != OK)
154*433d6423SLionel Sambuc 		return 0;
155*433d6423SLionel Sambuc 	return (unsigned long) m.m_lc_vm_getphys.ret_addr;
156*433d6423SLionel Sambuc }
157*433d6423SLionel Sambuc 
vm_getrefcount(endpoint_t endpt,void * addr)158*433d6423SLionel Sambuc u8_t vm_getrefcount(endpoint_t endpt, void *addr)
159*433d6423SLionel Sambuc {
160*433d6423SLionel Sambuc 	message m;
161*433d6423SLionel Sambuc 	int r;
162*433d6423SLionel Sambuc 
163*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
164*433d6423SLionel Sambuc 	m.m_lsys_vm_getref.endpt = endpt;
165*433d6423SLionel Sambuc 	m.m_lsys_vm_getref.addr  = addr;
166*433d6423SLionel Sambuc 
167*433d6423SLionel Sambuc 	r = _syscall(VM_PROC_NR, VM_GETREF, &m);
168*433d6423SLionel Sambuc 	if (r != OK)
169*433d6423SLionel Sambuc 		return (u8_t) -1;
170*433d6423SLionel Sambuc 	return (u8_t) m.m_lsys_vm_getref.retc;
171*433d6423SLionel Sambuc }
172*433d6423SLionel Sambuc 
173