xref: /illumos-gate/usr/src/lib/libc/amd64/gen/_stack_grow.S (revision fc910014e8a32a65612105835a10995f2c13d942)
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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26	.file	"_stack_grow.s"
27
28/*
29 * void *
30 * _stack_grow(void *addr)
31 * {
32 *	uintptr_t base = (uintptr_t)curthread->ul_ustack.ss_sp;
33 *	size_t size = curthread->ul_ustack.ss_size;
34 *
35 *	if (size > (uintptr_t)addr - base)
36 *		return (addr);
37 *
38 *	if (size == 0)
39 *		return (addr);
40 *
41 *	if (size > %sp - base)
42 *		%sp = base - STACK_ALIGN;
43 *
44 *	*((char *)(base - 1));
45 *
46 *	_lwp_kill(_lwp_self(), SIGSEGV);
47 * }
48 */
49
50#include "SYS.h"
51#include <../assym.h>
52
53	ENTRY(_stack_grow)
54	movq	%rdi, %rax
55	movq	%fs:UL_USTACK+SS_SP, %rcx
56	movq	%fs:UL_USTACK+SS_SIZE, %rdx
57	movq	%rax, %rbx
58	subq	%rcx, %rbx
59	cmpq	%rdx, %rbx
60	jae	1f
61	ret
621:
63	/
64	/ If the stack size is 0, stack checking is disabled.
65	/
66	cmpq	$0, %rdx
67	jne	2f
68	ret
692:
70	/
71	/ Move the stack pointer outside the stack bounds if it isn't already.
72	/
73	movq	%rsp, %rbx
74	subq	%rcx, %rbx
75	cmpq	%rdx, %rbx
76	jae	3f
77	pushq	%rbp
78	movq	%rsp, %rbp
79	movq	%rcx, %rsp
80	subq	$STACK_ALIGN, %rsp
813:
82	/
83	/ Dereference an address in the guard page.
84	/
85	movb	-1(%rcx), %bl
86
87	/
88	/ If the above load doesn't raise a SIGSEGV then do it ourselves.
89	/
90	SYSTRAP_RVAL1(lwp_self)
91	movl	$SIGSEGV, %esi
92	movl	%eax, %edi
93	SYSTRAP_RVAL1(lwp_kill)
94
95	/
96	/ Try one last time to take out the process.
97	/
98	movq	0x0, %rax
99	SET_SIZE(_stack_grow)
100