1/*	$NetBSD: uvm_unix.c,v 1.51 2022/01/10 18:04:20 christos Exp $	*/
2
3/*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993 The Regents of the University of California.
6 * Copyright (c) 1988 University of Utah.
7 *
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
12 * Science Department.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
39 *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
40 * from: Id: uvm_unix.c,v 1.1.2.2 1997/08/25 18:52:30 chuck Exp
41 */
42
43/*
44 * uvm_unix.c: traditional sbrk/grow interface to vm.
45 */
46
47#include <sys/cdefs.h>
48__KERNEL_RCSID(0, "$NetBSD: uvm_unix.c,v 1.51 2022/01/10 18:04:20 christos Exp $");
49
50#include "opt_pax.h"
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/proc.h>
55#include <sys/resourcevar.h>
56
57#include <sys/mount.h>
58#include <sys/syscallargs.h>
59#include <sys/pax.h>
60
61#include <uvm/uvm.h>
62
63/*
64 * sys_obreak: set break
65 */
66
67int
68sys_obreak(struct lwp *l, const struct sys_obreak_args *uap, register_t *retval)
69{
70	/* {
71		syscallarg(char *) nsize;
72	} */
73	struct proc *p = l->l_proc;
74	struct vmspace *vm = p->p_vmspace;
75	vaddr_t nbreak, obreak;
76	int error;
77
78	mutex_enter(&p->p_auxlock);
79	obreak = (vaddr_t)vm->vm_daddr;
80	nbreak = round_page((vaddr_t)SCARG(uap, nsize));
81	if (nbreak == 0
82	    || ((nbreak - obreak) > p->p_rlimit[RLIMIT_DATA].rlim_cur
83		&& nbreak > obreak)) {
84		mutex_exit(&p->p_auxlock);
85		return (ENOMEM);
86	}
87
88	obreak = round_page(obreak + ptoa(vm->vm_dsize));
89
90	if (nbreak == obreak) {
91		mutex_exit(&p->p_auxlock);
92		return (0);
93	}
94
95	/*
96	 * grow or shrink?
97	 */
98
99	if (nbreak > obreak) {
100		vm_prot_t prot = UVM_PROT_RW;
101		vm_prot_t maxprot;
102
103		maxprot = PAX_MPROTECT_MAXPROTECT(l, prot, 0, UVM_PROT_ALL);
104
105		error = uvm_map(&vm->vm_map, &obreak, nbreak - obreak, NULL,
106		    UVM_UNKNOWN_OFFSET, 0,
107		    UVM_MAPFLAG(prot, maxprot,
108				UVM_INH_COPY,
109				UVM_ADV_NORMAL, UVM_FLAG_AMAPPAD|UVM_FLAG_FIXED|
110				UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
111		if (error) {
112#ifdef DEBUG
113			uprintf("sbrk: grow %#"PRIxVADDR" failed, error = %d\n",
114			    nbreak - obreak, error);
115#endif
116			mutex_exit(&p->p_auxlock);
117			return (error);
118		}
119		vm->vm_dsize += atop(nbreak - obreak);
120	} else {
121		uvm_deallocate(&vm->vm_map, nbreak, obreak - nbreak);
122		vm->vm_dsize -= atop(obreak - nbreak);
123	}
124	mutex_exit(&p->p_auxlock);
125
126	return (0);
127}
128
129/*
130 * uvm_grow: enlarge the "stack segment" to include sp.
131 */
132
133int
134uvm_grow(struct proc *p, vaddr_t sp)
135{
136	struct vmspace *vm = p->p_vmspace;
137	vsize_t nss;
138
139	/*
140	 * For user defined stacks (from sendsig).
141	 */
142#ifdef __MACHINE_STACK_GROWS_UP
143	if (sp < (vaddr_t)vm->vm_minsaddr)
144#else
145	if (sp < (vaddr_t)vm->vm_maxsaddr)
146#endif
147		return (0);
148
149	/*
150	 * For common case of already allocated (from trap).
151	 */
152#ifdef __MACHINE_STACK_GROWS_UP
153	if (sp < p->p_stackbase + ctob(vm->vm_ssize))
154#else
155	if (sp >= p->p_stackbase - ctob(vm->vm_ssize))
156#endif
157		return (1);
158
159	/*
160	 * Really need to check vs limit and increment stack size if ok.
161	 */
162#ifdef __MACHINE_STACK_GROWS_UP
163	nss = btoc(sp - p->p_stackbase);
164#else
165	nss = btoc(p->p_stackbase - sp);
166#endif
167	if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur))
168		return (0);
169	vm->vm_ssize = nss;
170	return (1);
171}
172
173/*
174 * sys_oadvise: old advice system call
175 */
176
177/* ARGSUSED */
178int
179sys_ovadvise(struct lwp *l, const struct sys_ovadvise_args *uap, register_t *retval)
180{
181#if 0
182	/* {
183		syscallarg(int) anom;
184	} */
185#endif
186
187	return (EINVAL);
188}
189