1/* longjmp with sp check for C-SKY ABIV2.
2   Copyright (C) 2018-2021 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library.  If not, see
17   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20
21	.section .rodata.str1.8,"aMS",@progbits,1
22	.align  2
23	.type   longjmp_msg,@object
24longjmp_msg:
25	.string "longjmp causes uninitialized stack frame"
26	.size   longjmp_msg, .-longjmp_msg
27
28	.text
29#define __longjmp ____longjmp_chk
30
31#ifdef __PIC__
32# define CALL_FAIL	\
33	subi	sp, 8;						\
34	stw	gb, (sp, 0);					\
35	grs	gb, .Lgetpc1;					\
36.Lgetpc1:							\
37	lrw	t0, .Lgetpc1@GOTPC;				\
38	addu	gb, gb, t0;					\
39	lrw	a0, longjmp_msg@GOTOFF;				\
40	addu	a0, a0, gb;					\
41	lrw	t0, (HIDDEN_JUMPTARGET (__fortify_fail))@PLT;	\
42	ldr.w	t0, (gb, t0 << 0);				\
43	jsr	t0;						\
44	ldw	gb, (sp, 0);					\
45	addi	sp, 8;
46#else /* __PIC__ */
47# define CALL_FAIL	\
48	lrw	a0, longjmp_msg;				\
49	jsri	HIDDEN_JUMPTARGET (__fortify_fail);
50#endif /* __PIC__ */
51
52# define CHECK_SP(reg)	\
53	cmplt	sp, reg;					\
54	bt	.Lok1;						\
55	mov	r9, a0;						\
56	mov	t0, r7;						\
57	lrw	r7, __NR_sigaltstack;				\
58	movi	a0, 0;						\
59	subi	sp, 12;	/* sizeof (stack_t) */			\
60	mov	a1, sp;						\
61	trap	0;						\
62	mov	r7, t0;						\
63	cmpnei	a0, 0;						\
64	bt	.Lok;						\
65	ldw	a1, (sp, 4);					\
66	btsti	a1, 1;						\
67	bt	.Lfail;						\
68	ldw	a1, (sp, 0);					\
69	ldw	a3, (sp, 8);					\
70	add	a1, a3;						\
71	sub	a1, reg;					\
72	cmphs	a1, a3;						\
73	bt	.Lok;						\
74.Lfail:								\
75	addi	sp, 12;						\
76	CALL_FAIL						\
77.Lok:								\
78	mov	a0, r9;						\
79.Lok1:
80
81#include <__longjmp.S>
82