asmacros.h revision 216802
180709Sjake/*-
280709Sjake * Copyright (c) 2001 Jake Burkholder.
380709Sjake * All rights reserved.
480709Sjake *
580709Sjake * Redistribution and use in source and binary forms, with or without
680709Sjake * modification, are permitted provided that the following conditions
780709Sjake * are met:
880709Sjake * 1. Redistributions of source code must retain the above copyright
980709Sjake *    notice, this list of conditions and the following disclaimer.
1080709Sjake * 2. Redistributions in binary form must reproduce the above copyright
1180709Sjake *    notice, this list of conditions and the following disclaimer in the
1280709Sjake *    documentation and/or other materials provided with the distribution.
1380709Sjake *
1481334Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1580709Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1680709Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781334Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1880709Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1980709Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2080709Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2180709Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2280709Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2380709Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2480709Sjake * SUCH DAMAGE.
2580709Sjake *
2680709Sjake * $FreeBSD: head/sys/sparc64/include/asmacros.h 216802 2010-12-29 14:14:50Z marius $
2780709Sjake */
2880709Sjake
2980709Sjake#ifndef	_MACHINE_ASMACROS_H_
3080709Sjake#define	_MACHINE_ASMACROS_H_
3180709Sjake
3280709Sjake#ifdef _KERNEL
3380709Sjake
3488616Sjake/*
3589032Sjake * Normal and alternate %g6 point to the pcb of the current process.  Normal,
36216802Smarius * alternate and interrupt %g7 point to per-cpu data.
3788616Sjake */
3889032Sjake#define	PCB_REG		%g6
3988616Sjake#define	PCPU_REG	%g7
4081893Sjake
4188616Sjake/*
4289032Sjake * Alternate %g5 points to a per-cpu panic stack, which is used as a last
4389032Sjake * resort, and for temporarily saving alternate globals.
4488616Sjake */
4588616Sjake#define	ASP_REG		%g5
4688616Sjake
4789032Sjake#ifdef LOCORE
4889032Sjake
4988616Sjake/*
5089032Sjake * Atomically decrement an integer in memory.
5188616Sjake */
5289032Sjake#define	ATOMIC_DEC_INT(r1, r2, r3) \
5389032Sjake	lduw	[r1], r2 ; \
5489032Sjake9:	sub	r2, 1, r3 ; \
5589032Sjake	casa	[r1] ASI_N, r2, r3 ; \
5689032Sjake	cmp	r2, r3 ; \
5792198Sjake	bne,pn	%icc, 9b ; \
5889032Sjake	 mov	r3, r2
5988616Sjake
6089032Sjake/*
6189032Sjake * Atomically increment an integer in memory.
6289032Sjake */
6389032Sjake#define	ATOMIC_INC_INT(r1, r2, r3) \
6489032Sjake	lduw	[r1], r2 ; \
6589032Sjake9:	add	r2, 1, r3 ; \
6689032Sjake	casa	[r1] ASI_N, r2, r3 ; \
6789032Sjake	cmp	r2, r3 ; \
6892198Sjake	bne,pn	%icc, 9b ; \
6989032Sjake	 mov	r3, r2
7089032Sjake
7192198Sjake/*
72117658Sjmg * Atomically increment an u_long in memory.
73117658Sjmg */
74117658Sjmg#define	ATOMIC_INC_ULONG(r1, r2, r3) \
75117658Sjmg	ldx	[r1], r2 ; \
76117658Sjmg9:	add	r2, 1, r3 ; \
77117658Sjmg	casxa	[r1] ASI_N, r2, r3 ; \
78117658Sjmg	cmp	r2, r3 ; \
79117658Sjmg	bne,pn	%icc, 9b ; \
80117658Sjmg	 mov	r3, r2
81117658Sjmg
82117658Sjmg/*
8392198Sjake * Atomically clear a number of bits of an integer in memory.
8492198Sjake */
8592198Sjake#define	ATOMIC_CLEAR_INT(r1, r2, r3, bits) \
8692198Sjake	lduw	[r1], r2 ; \
8792198Sjake9:	andn	r2, bits, r3 ; \
8892198Sjake	casa	[r1] ASI_N, r2, r3 ; \
8992198Sjake	cmp	r2, r3 ; \
9092198Sjake	bne,pn	%icc, 9b ; \
9192198Sjake	 mov	r3, r2
9292198Sjake
9397262Sjake#define	PCPU(member)	PCPU_REG + PC_ ## member
9497262Sjake#define	PCPU_ADDR(member, reg) \
9597262Sjake	add	PCPU_REG, PC_ ## member, reg
9684176Sjake
9797262Sjake#define	DEBUGGER() \
9897262Sjake	ta	%xcc, 1
9984176Sjake
10088616Sjake#define	PANIC(msg, r1) \
10180709Sjake	.sect	.rodata ; \
10280709Sjake9:	.asciz	msg ; \
10380709Sjake	.previous ; \
104130164Sphk	SET(9b, r1, %o0) ; \
105130164Sphk	call	panic ; \
10680709Sjake	 nop
10780709Sjake
10889032Sjake#ifdef INVARIANTS
10989032Sjake#define	KASSERT(r1, msg) \
110203846Smarius	brnz,pt	r1, 8f ; \
11189032Sjake	 nop ; \
11289032Sjake	PANIC(msg, r1) ; \
11389032Sjake8:
11489032Sjake#else
11589032Sjake#define	KASSERT(r1, msg)
11689032Sjake#endif
11789032Sjake
11888616Sjake#define	PUTS(msg, r1) \
11988616Sjake	.sect	.rodata ; \
12088616Sjake9:	.asciz	msg ; \
12188616Sjake	.previous ; \
12288616Sjake	SET(9b, r1, %o0) ; \
12388616Sjake	call	printf ; \
12488616Sjake	 nop
12588616Sjake
126100840Sjake#define	_ALIGN_DATA	.align 8
127100840Sjake
12880709Sjake#define	DATA(name) \
12980709Sjake	.data ; \
130100840Sjake	_ALIGN_DATA ; \
13180709Sjake	.globl	name ; \
13280709Sjake	.type	name, @object ; \
133114071Sobrienname:
13480709Sjake
13580709Sjake#define	EMPTY
13680709Sjake
137216802Smarius/*
138216802Smarius * Generate atomic compare and swap, load and store instructions for the
139216802Smarius * corresponding width and ASI (or not).  Note that we want to evaluate the
140216802Smarius * macro args before concatenating, so that EMPTY really turns into nothing.
141216802Smarius */
142216802Smarius#define		_LD(w, a)	ld ## w ## a
143216802Smarius#define		_ST(w, a)	st ## w ## a
144216802Smarius#define		_CAS(w, a)	cas ## w ## a
145216802Smarius
146216802Smarius#define		LD(w, a)	_LD(w, a)
147216802Smarius#define		ST(w, a)	_ST(w, a)
148216802Smarius#define		CAS(w, a)	_CAS(w, a)
149216802Smarius
15092213Sjake#endif /* LOCORE */
15192213Sjake
15292213Sjake#endif /* _KERNEL */
15392213Sjake
15480709Sjake#endif /* !_MACHINE_ASMACROS_H_ */
155