summaryrefslogtreecommitdiff
path: root/sys/arch/sh5/include/asm.h
blob: 2c5a6dc9009e5d8bf0fd2e4c9df8d4f7f5090a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*	$NetBSD: asm.h,v 1.10 2003/08/10 22:18:12 scw Exp $	*/

/*
 * Copyright 2002 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Steve C. Woodford for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)asm.h	5.5 (Berkeley) 5/7/91
 */

#ifndef _SH5_ASM_H
#define	_SH5_ASM_H

#ifdef __NO_LEADING_UNDERSCORES__
# define _C_LABEL(x)	x
#else
# if __STDC__
#  define _C_LABEL(x)	_ ## x
# else
#  define _C_LABEL(x)	_/**/x
# endif
#endif

#define _ASM_LABEL(x)	x

/* let kernels and others override entrypoint alignment */
#ifndef _ALIGN_TEXT
# define _ALIGN_TEXT .align 5
#endif

/* The text sections for 32 and 64 bit code are different ... */
#ifndef _LP64
#define	_TEXT_SECTION	.section .text..SHmedia32,"ax"
#else
#define	_TEXT_SECTION	.text
#endif

/*
 * Building block for C-callable entry points
 */
#define	_ENTRY(x)							\
	_TEXT_SECTION							;\
	_ALIGN_TEXT							;\
	.globl x							;\
	.type x,@function						;\
	x:

/*
 * Building block for Assembly-callable entry points
 */
#define	_ASENTRY(x)							\
	_TEXT_SECTION							;\
	_ALIGN_TEXT							;\
	.globl x							;\
	.type x,@function						;\
	x:

/*
 * When profiling, the following instructions are included in
 * the function prologue...
 */
#ifdef GPROF
#ifdef PIC
#define	_PROF_PROLOGUE	pta/l	__mcount@PLT, tr0; blink tr0, r0
#else
#define	_PROF_PROLOGUE	pta/l	__mcount, tr0; blink tr0, r0
#endif
#else
#define	_PROF_PROLOGUE
#endif

/*
 * C-callable entry point, with profiling enabled.
 */
#define	ENTRY(y)		_ENTRY(_C_LABEL(y))			;\
				_PROF_PROLOGUE

/*
 * C-callable entry point, without profiling.
 */
#define	ENTRY_NOPROFILE(y)	_ENTRY(_C_LABEL(y))

/*
 * Alternative name for a C-callable entry point.
 */
#define	ALTENTRY(y)		_ENTRY(_C_LABEL(y))

/*
 * Profiled entry point, callable only from assembly code
 */
#define	ASENTRY(y)		_ASENTRY(_ASM_LABEL(y))			;\
				_PROF_PROLOGUE

/*
 * Non-profiled entry point, callable only from assembly code
 */
#define	ASENTRY_NOPROFILE(y)	_ASENTRY(_ASM_LABEL(y))


/*
 * Global variables
 */
#define	GLOBAL(x)	.globl	_C_LABEL(x) ;\
		_C_LABEL(x):

#define	ASGLOBAL(x)	.globl	_ASM_LABEL(x) ;\
		_ASM_LABEL(x):

/*
 * ... and local variables.
 */
#define	LOCAL(x)	_C_LABEL(x):
#define	ASLOCAL(x)	_ASM_LABEL(x):

/*
 * Items in the BSS segment.
 */
#define	BSS(name, size)		.comm	_C_LABEL(name),size
#define	ASBSS(name, size)	.comm	_ASM_LABEL(name,size

#define	ASMSTR		.asciz

#define	RCSID(x)	.text; .asciz x

#define	WEAK_ALIAS(alias,sym)						\
	.weak _C_LABEL(alias);						\
	_C_LABEL(alias) = _C_LABEL(sym)

#ifdef __STDC__
#define	__STRING(x)			#x
#define	WARN_REFERENCES(sym, msg)
#else
#define	__STRING(x)			"x"
#define	WARN_REFERENCES(sym, msg)
#endif /* __STDC__ */

/*
 * Helper macroes for dealing with ILP32/LP64 issues in assembly code
 */

/*
 * Load a 32-bit constant `v' into register `reg', with sign-extension
 * to 64-bits.
 *
 * Use the first if the constant has bit-31 clear, otherwise use the 2nd.
 */
#define	LDC32(v, reg)		\
	movi	(((v) >> 16) & 65535), reg	;\
	shori	((v) & 65535), reg

#define	LDSC32(v, reg)		\
	movi	(((v) >> 16) & 65535) - 65536, reg	;\
	shori	((v) & 65535), reg

/*
 * Ditto, without sign-extension
 */
#define	LDUC32(v, reg)		\
	movi	0, reg				;\
	shori	((v) >> 16) & 65535, reg	;\
	shori	(v) & 65535, reg

/*
 * Load a 64-bit constance `v' into register `reg'.
 */
#define	LDC64(v, reg)		\
	movi	(((v) >> 48) & 65535), reg	;\
	shori	(((v) >> 32) & 65535), reg	;\
	shori	(((v) >> 16) & 65535), reg	;\
	shori	((v) & 65535), reg

#define	LDSC64(v, reg)		\
	movi	(((v) >> 48) & 65535) - 65536, reg	;\
	shori	(((v) >> 32) & 65535), reg	;\
	shori	(((v) >> 16) & 65535), reg	;\
	shori	((v) & 65535), reg


#ifndef _LP64
/*
 * Load the effective address of a data object into a register
 */
#define	LEA(sym, reg)		LDC32(datalabel (sym), reg)

/*
 * Ditto, but used for loading a relative address
 */
#define	LEAR(sym, label, reg)						\
	movi	(((datalabel sym - (label - .)) >> 16) & 65535), reg;	\
	shori	((datalabel sym - (label - .)) & 65535), reg

/*
 * Load the effective address of a function into a register.
 */
#define	LEAF(sym, reg)		LDC32(sym, reg)

/*
 * Ditto, but used for loading a relative address
 */
#define	LEARF(fn, label, reg)					\
	movi	(((fn - (label - .)) >> 16) & 65535), reg;	\
	shori	((fn - (label - .)) & 65535), reg

/*
 * Instruction to load a pointer
 */
#define	LDPTR			ld.l
#define	LDXPTR			ldx.l

/*
 * Instruction to store a pointer
 */
#define	STPTR			st.l
#define	STXPTR			stx.l

/*
 * Set up/Clear down a C-like stack frame on entry/exit to an assembly function
 */
#define	LINK_FRAME(sz)			\
	addi.l	r15, -(8 + (sz)), r15	;\
	st.l	r15, (sz), r14		;\
	st.l	r15, (sz) + 4, r18	;\
	add.l	r15, r63, r14

#define	UNLINK_FRAME(sz)		\
	add.l	r14, r63, r15		;\
	ld.l	r15, (sz), r14		;\
	ld.l	r15, (sz) + 4, r18	;\
	addi.l	r15, (sz) + 8, r15

/*
 * For stack frames sized between 512 and 32767 bytes, inclusive.
 */
#define	LINK_BIG_FRAME(sz)		\
	addi.l	r15, -8, r15		;\
	movi	sz, r0			;\
	st.l	r15, 0, r14		;\
	st.l	r15, 4, r18		;\
	sub.l	r15, r0, r15		;\
	add.l	r15, r63, r14

#define	UNLINK_BIG_FRAME(sz)		\
	movi	sz, r0			;\
	add.l	r14, r0, r15		;\
	ld.l	r15, 0, r14		;\
	ld.l	r15, 4, r18		;\
	addi.l	r15, 8, r15

#else

/*
 * 64-bit versions of the above macroes....
 */

#define	LEA(sym, reg)		LDC64(datalabel (sym), reg)
#define	LEAR(sym, label, reg)						\
	movi	(((datalabel sym - (label - .)) >> 48) & 65535), reg;	\
	movi	(((datalabel sym - (label - .)) >> 32) & 65535), reg;	\
	movi	(((datalabel sym - (label - .)) >> 16) & 65535), reg;	\
	shori	((datalabel sym - (label - .)) & 65535), reg
#define	LEAF(sym, reg)		LDC64(sym, reg)
#define	LEARF(fn, label, reg)					\
	movi	(((fn - (label - .)) >> 48) & 65535), reg;	\
	movi	(((fn - (label - .)) >> 32) & 65535), reg;	\
	movi	(((fn - (label - .)) >> 16) & 65535), reg;	\
	shori	((fn - (label - .)) & 65535), reg
#define	LDPTR			ld.q
#define	LDXPTR			ldx.q
#define	STPTR			st.q
#define	STXPTR			stx.q
#define	LINK_FRAME(sz)			\
	addi	r15, -(16 + (sz)), r15	;\
	st.q	r15, (sz), r14		;\
	st.q	r15, (sz) + 8, r18	;\
	add	r15, r63, r14

#define	UNLINK_FRAME(sz)		\
	add	r14, r63, r15		;\
	ld.q	r15, (sz), r14		;\
	ld.q	r15, (sz) + 8, r18	;\
	addi	r15, (sz) + 16, r15

#define	LINK_BIG_FRAME(sz)		\
	addi	r15, -16, r15		;\
	movi	sz, r0			;\
	st.q	r15, 0, r14		;\
	st.q	r15, 8, r18		;\
	sub	r15, r0, r15		;\
	add	r15, r63, r14

#define	UNLINK_BIG_FRAME(sz)		\
	movi	sz, r0			;\
	add	r14, r0, r15		;\
	ld.q	r15, 0, r14		;\
	ld.q	r15, 8, r18		;\
	addi	r15, 16, r15
#endif

#ifndef PIC
#define	PIC_PROLOGUE
#define	PIC_EPILOGUE
#define	PIC_GET_GOT(tr)
#define	PIC_LEAF(x, r)					\
	LEAF(x, r)
#define	PIC_LEA(x, r)					\
	LEA(x, r)
#define	PIC_PTAL(x, tmp, tr)				\
	pta/l	x, tr
#define	PIC_PTAU(x, tmp, tr)				\
	pta/u	x, tr
#else
#define	PIC_GET_GOT(tr)					\
	LEAR(_GLOBAL_OFFSET_TABLE_, 666f, r12)		;\
666:	ptrel/u	r12, tr					;\
	gettr	tr, r12
#define	PIC_PROLOGUE					\
	addi	r15, -8, r15				;\
	st.q	r15, 0, r12				;\
	PIC_GET_GOT(tr0)
#define	PIC_EPILOGUE					\
	ld.q	r15, 0, r12				;\
	addi	r15, 8, r15
#define	PIC_LEAF(x, r)					\
	LEAF(x@GOTPLT, r)				;\
	LDXPTR	r12, r, r
#define	PIC_LEA(x, r)					\
	LEA(x@GOT, r)					;\
	LDXPTR	r12, r, r
#define	PIC_PTAL(x, tmp, tr)				\
	PIC_LEAF(x, tmp)				;\
	ptabs/l	tmp, tr
#define	PIC_PTAU(x, tmp, tr)				\
	PIC_LEAF(x, tmp)				;\
	ptabs/u	tmp, tr
#endif


/*
 * Used in assembly code to convert a pteg hash into the offset from
 * the base of the pmap_pteg_table.
 *
 * When SH5_NEFF_BITS = 32, sizeof(pteg_t) == 96
 * When SH5_NEFF_BITS > 32, sizeof(pteg_t) == 192
 *
 * XXX: should really be in <sh5/pte.h>
 */
#ifdef _KERNEL
#if SH5_NEFF_BITS == 32
#define	HASH_TO_PTEG_IDX(r,t)	\
	shlli	r, 5, t		/* t = r * 32 */	;\
	shlli	r, 6, r		/* r = r * 64 */	;\
	add	r, t, r		/* r = r + t  */
#define	LDPTE	ld.l
#define	STPTE	st.l
#else
#define	HASH_TO_PTEG_IDX(r,t)	\
	shlli	r, 6, t		/* t = r * 64  */	;\
	shlli	r, 7, r		/* r = r * 128 */	;\
	add	r, t, r		/* r = r + t   */
#define	LDPTE	ld.q
#define	STPTE	st.q
#endif
#endif	/* _KERNEL */

#endif /* _SH5_ASM_H */