1*bd50262fSKonstantin Belousov /* -*- mode: asm -*- */ 23c4dd356SDavid Greenman /*- 351369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 451369649SPedro F. Giffuni * 53c4dd356SDavid Greenman * Copyright (c) 1993 The Regents of the University of California. 63c4dd356SDavid Greenman * All rights reserved. 73c4dd356SDavid Greenman * 8*bd50262fSKonstantin Belousov * Copyright (c) 2018 The FreeBSD Foundation 9*bd50262fSKonstantin Belousov * All rights reserved. 10*bd50262fSKonstantin Belousov * 11*bd50262fSKonstantin Belousov * Portions of this software were developed by 12*bd50262fSKonstantin Belousov * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from 13*bd50262fSKonstantin Belousov * the FreeBSD Foundation. 14*bd50262fSKonstantin Belousov * 153c4dd356SDavid Greenman * Redistribution and use in source and binary forms, with or without 163c4dd356SDavid Greenman * modification, are permitted provided that the following conditions 173c4dd356SDavid Greenman * are met: 183c4dd356SDavid Greenman * 1. Redistributions of source code must retain the above copyright 193c4dd356SDavid Greenman * notice, this list of conditions and the following disclaimer. 203c4dd356SDavid Greenman * 2. Redistributions in binary form must reproduce the above copyright 213c4dd356SDavid Greenman * notice, this list of conditions and the following disclaimer in the 223c4dd356SDavid Greenman * documentation and/or other materials provided with the distribution. 23fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 243c4dd356SDavid Greenman * may be used to endorse or promote products derived from this software 253c4dd356SDavid Greenman * without specific prior written permission. 263c4dd356SDavid Greenman * 273c4dd356SDavid Greenman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 283c4dd356SDavid Greenman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 293c4dd356SDavid Greenman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 303c4dd356SDavid Greenman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 313c4dd356SDavid Greenman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 323c4dd356SDavid Greenman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 333c4dd356SDavid Greenman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 343c4dd356SDavid Greenman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 353c4dd356SDavid Greenman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 363c4dd356SDavid Greenman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 373c4dd356SDavid Greenman * SUCH DAMAGE. 383c4dd356SDavid Greenman * 39c3aac50fSPeter Wemm * $FreeBSD$ 403c4dd356SDavid Greenman */ 413c4dd356SDavid Greenman 42ab9678acSBruce Evans #ifndef _MACHINE_ASMACROS_H_ 43ab9678acSBruce Evans #define _MACHINE_ASMACROS_H_ 44ab9678acSBruce Evans 45ee323f62SPeter Wemm #include <sys/cdefs.h> 466605d9f3SJordan K. Hubbard 47435929a8SBruce Evans /* XXX too much duplication in various asm*.h's. */ 48912e6037SBruce Evans 499081eec1SJohn Polstra /* 5094450a83SBruce Evans * CNAME is used to manage the relationship between symbol names in C 519081eec1SJohn Polstra * and the equivalent assembly language names. CNAME is given a name as 529081eec1SJohn Polstra * it would be used in a C program. It expands to the equivalent assembly 5394450a83SBruce Evans * language name. 549081eec1SJohn Polstra */ 559081eec1SJohn Polstra #define CNAME(csym) csym 560967373eSDavid Greenman 57afa88623SPeter Wemm #define ALIGN_DATA .p2align 3 /* 8 byte alignment, zero filled */ 589081eec1SJohn Polstra #ifdef GPROF 599081eec1SJohn Polstra #define ALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */ 609081eec1SJohn Polstra #else 61afa88623SPeter Wemm #define ALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */ 629081eec1SJohn Polstra #endif 639081eec1SJohn Polstra #define SUPERALIGN_TEXT .p2align 4,0x90 /* 16-byte alignment, nop filled */ 649081eec1SJohn Polstra 659081eec1SJohn Polstra #define GEN_ENTRY(name) ALIGN_TEXT; .globl CNAME(name); \ 66ea2b3e3dSBruce Evans .type CNAME(name),@function; CNAME(name): 67912e6037SBruce Evans #define NON_GPROF_ENTRY(name) GEN_ENTRY(name) 68435929a8SBruce Evans #define NON_GPROF_RET .byte 0xc3 /* opcode for `ret' */ 69d2306226SDavid Greenman 70298889efSJoseph Koshy #define END(name) .size name, . - name 71298889efSJoseph Koshy 720967373eSDavid Greenman #ifdef GPROF 730967373eSDavid Greenman /* 74ea2b3e3dSBruce Evans * __mcount is like [.]mcount except that doesn't require its caller to set 75912e6037SBruce Evans * up a frame pointer. It must be called before pushing anything onto the 76912e6037SBruce Evans * stack. gcc should eventually generate code to call __mcount in most 77912e6037SBruce Evans * cases. This would make -pg in combination with -fomit-frame-pointer 78912e6037SBruce Evans * useful. gcc has a configuration variable PROFILE_BEFORE_PROLOGUE to 79912e6037SBruce Evans * allow profiling before setting up the frame pointer, but this is 80912e6037SBruce Evans * inadequate for good handling of special cases, e.g., -fpic works best 81912e6037SBruce Evans * with profiling after the prologue. 82912e6037SBruce Evans * 83ea2b3e3dSBruce Evans * [.]mexitcount is a new function to support non-statistical profiling if an 84435929a8SBruce Evans * accurate clock is available. For C sources, calls to it are generated 85435929a8SBruce Evans * by the FreeBSD extension `-mprofiler-epilogue' to gcc. It is best to 86ea2b3e3dSBruce Evans * call [.]mexitcount at the end of a function like the MEXITCOUNT macro does, 87435929a8SBruce Evans * but gcc currently generates calls to it at the start of the epilogue to 88435929a8SBruce Evans * avoid problems with -fpic. 89912e6037SBruce Evans * 90ea2b3e3dSBruce Evans * [.]mcount and __mcount may clobber the call-used registers and %ef. 91ea2b3e3dSBruce Evans * [.]mexitcount may clobber %ecx and %ef. 92912e6037SBruce Evans * 93435929a8SBruce Evans * Cross-jumping makes non-statistical profiling timing more complicated. 94ea2b3e3dSBruce Evans * It is handled in many cases by calling [.]mexitcount before jumping. It 95ea2b3e3dSBruce Evans * is handled for conditional jumps using CROSSJUMP() and CROSSJUMP_LABEL(). 96435929a8SBruce Evans * It is handled for some fault-handling jumps by not sharing the exit 97435929a8SBruce Evans * routine. 98912e6037SBruce Evans * 99912e6037SBruce Evans * ALTENTRY() must be before a corresponding ENTRY() so that it can jump to 100912e6037SBruce Evans * the main entry point. Note that alt entries are counted twice. They 101912e6037SBruce Evans * have to be counted as ordinary entries for gprof to get the call times 102912e6037SBruce Evans * right for the ordinary entries. 103912e6037SBruce Evans * 104912e6037SBruce Evans * High local labels are used in macros to avoid clashes with local labels 105912e6037SBruce Evans * in functions. 106912e6037SBruce Evans * 107435929a8SBruce Evans * Ordinary `ret' is used instead of a macro `RET' because there are a lot 108435929a8SBruce Evans * of `ret's. 0xc3 is the opcode for `ret' (`#define ret ... ret' can't 109435929a8SBruce Evans * be used because this file is sometimes preprocessed in traditional mode). 110435929a8SBruce Evans * `ret' clobbers eflags but this doesn't matter. 1110967373eSDavid Greenman */ 112912e6037SBruce Evans #define ALTENTRY(name) GEN_ENTRY(name) ; MCOUNT ; MEXITCOUNT ; jmp 9f 113435929a8SBruce Evans #define CROSSJUMP(jtrue, label, jfalse) \ 114435929a8SBruce Evans jfalse 8f; MEXITCOUNT; jmp __CONCAT(to,label); 8: 115435929a8SBruce Evans #define CROSSJUMPTARGET(label) \ 116435929a8SBruce Evans ALIGN_TEXT; __CONCAT(to,label): ; MCOUNT; jmp label 117912e6037SBruce Evans #define ENTRY(name) GEN_ENTRY(name) ; 9: ; MCOUNT 118cda07865SPeter Wemm #define FAKE_MCOUNT(caller) pushq caller ; call __mcount ; popq %rcx 119912e6037SBruce Evans #define MCOUNT call __mcount 120912e6037SBruce Evans #define MCOUNT_LABEL(name) GEN_ENTRY(name) ; nop ; ALIGN_TEXT 12144764790SBruce Evans #ifdef GUPROF 12294450a83SBruce Evans #define MEXITCOUNT call .mexitcount 123435929a8SBruce Evans #define ret MEXITCOUNT ; NON_GPROF_RET 12444764790SBruce Evans #else 12544764790SBruce Evans #define MEXITCOUNT 12644764790SBruce Evans #endif 127435929a8SBruce Evans 128435929a8SBruce Evans #else /* !GPROF */ 1290967373eSDavid Greenman /* 1300967373eSDavid Greenman * ALTENTRY() has to align because it is before a corresponding ENTRY(). 1310967373eSDavid Greenman * ENTRY() has to align to because there may be no ALTENTRY() before it. 132912e6037SBruce Evans * If there is a previous ALTENTRY() then the alignment code for ENTRY() 133912e6037SBruce Evans * is empty. 1340967373eSDavid Greenman */ 135912e6037SBruce Evans #define ALTENTRY(name) GEN_ENTRY(name) 136435929a8SBruce Evans #define CROSSJUMP(jtrue, label, jfalse) jtrue label 137435929a8SBruce Evans #define CROSSJUMPTARGET(label) 138912e6037SBruce Evans #define ENTRY(name) GEN_ENTRY(name) 139912e6037SBruce Evans #define FAKE_MCOUNT(caller) 140d2306226SDavid Greenman #define MCOUNT 141912e6037SBruce Evans #define MCOUNT_LABEL(name) 142912e6037SBruce Evans #define MEXITCOUNT 143912e6037SBruce Evans #endif /* GPROF */ 1440967373eSDavid Greenman 14518e3d9f5SScott Long /* 14618e3d9f5SScott Long * Convenience for adding frame pointers to hand-coded ASM. Useful for 14718e3d9f5SScott Long * DTrace, HWPMC, and KDB. 14818e3d9f5SScott Long */ 14918e3d9f5SScott Long #define PUSH_FRAME_POINTER \ 15018e3d9f5SScott Long pushq %rbp ; \ 15118e3d9f5SScott Long movq %rsp, %rbp ; 15218e3d9f5SScott Long #define POP_FRAME_POINTER \ 15318e3d9f5SScott Long popq %rbp 15418e3d9f5SScott Long 1550d2a2989SPeter Wemm #ifdef LOCORE 1560d2a2989SPeter Wemm /* 157333b8de5SJohn Baldwin * Access per-CPU data. 158333b8de5SJohn Baldwin */ 159333b8de5SJohn Baldwin #define PCPU(member) %gs:PC_ ## member 160333b8de5SJohn Baldwin #define PCPU_ADDR(member, reg) \ 161333b8de5SJohn Baldwin movq %gs:PC_PRVSPACE, reg ; \ 162333b8de5SJohn Baldwin addq $PC_ ## member, reg 163333b8de5SJohn Baldwin 164*bd50262fSKonstantin Belousov /* 165*bd50262fSKonstantin Belousov * Convenience macro for declaring interrupt entry points. 166*bd50262fSKonstantin Belousov */ 167*bd50262fSKonstantin Belousov #define IDTVEC(name) ALIGN_TEXT; .globl __CONCAT(X,name); \ 168*bd50262fSKonstantin Belousov .type __CONCAT(X,name),@function; __CONCAT(X,name): 169*bd50262fSKonstantin Belousov 170*bd50262fSKonstantin Belousov .macro SAVE_SEGS 171*bd50262fSKonstantin Belousov movw %fs,TF_FS(%rsp) 172*bd50262fSKonstantin Belousov movw %gs,TF_GS(%rsp) 173*bd50262fSKonstantin Belousov movw %es,TF_ES(%rsp) 174*bd50262fSKonstantin Belousov movw %ds,TF_DS(%rsp) 175*bd50262fSKonstantin Belousov .endm 176*bd50262fSKonstantin Belousov 177*bd50262fSKonstantin Belousov .macro MOVE_STACKS qw 178*bd50262fSKonstantin Belousov offset=0 179*bd50262fSKonstantin Belousov .rept \qw 180*bd50262fSKonstantin Belousov movq offset(%rsp),%rdx 181*bd50262fSKonstantin Belousov movq %rdx,offset(%rax) 182*bd50262fSKonstantin Belousov offset=offset+8 183*bd50262fSKonstantin Belousov .endr 184*bd50262fSKonstantin Belousov .endm 185*bd50262fSKonstantin Belousov 186*bd50262fSKonstantin Belousov .macro PTI_UENTRY has_err 187*bd50262fSKonstantin Belousov swapgs 188*bd50262fSKonstantin Belousov pushq %rax 189*bd50262fSKonstantin Belousov pushq %rdx 190*bd50262fSKonstantin Belousov movq PCPU(KCR3),%rax 191*bd50262fSKonstantin Belousov movq %rax,%cr3 192*bd50262fSKonstantin Belousov movq PCPU(RSP0),%rax 193*bd50262fSKonstantin Belousov subq $PTI_SIZE,%rax 194*bd50262fSKonstantin Belousov MOVE_STACKS (PTI_SIZE / 8) - 1 + \has_err 195*bd50262fSKonstantin Belousov movq %rax,%rsp 196*bd50262fSKonstantin Belousov popq %rdx 197*bd50262fSKonstantin Belousov popq %rax 198*bd50262fSKonstantin Belousov .endm 199*bd50262fSKonstantin Belousov 200*bd50262fSKonstantin Belousov .macro PTI_ENTRY name, cont, has_err=0 201*bd50262fSKonstantin Belousov ALIGN_TEXT 202*bd50262fSKonstantin Belousov .globl X\name\()_pti 203*bd50262fSKonstantin Belousov .type X\name\()_pti,@function 204*bd50262fSKonstantin Belousov X\name\()_pti: 205*bd50262fSKonstantin Belousov /* %rax, %rdx and possibly err not yet pushed */ 206*bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-(2+1-\has_err)*8(%rsp) 207*bd50262fSKonstantin Belousov jz \cont 208*bd50262fSKonstantin Belousov PTI_UENTRY \has_err 209*bd50262fSKonstantin Belousov swapgs 210*bd50262fSKonstantin Belousov jmp \cont 211*bd50262fSKonstantin Belousov .endm 212*bd50262fSKonstantin Belousov 213*bd50262fSKonstantin Belousov .macro PTI_INTRENTRY vec_name 214*bd50262fSKonstantin Belousov SUPERALIGN_TEXT 215*bd50262fSKonstantin Belousov .globl X\vec_name\()_pti 216*bd50262fSKonstantin Belousov .type X\vec_name\()_pti,@function 217*bd50262fSKonstantin Belousov X\vec_name\()_pti: 218*bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* err, %rax, %rdx not pushed */ 219*bd50262fSKonstantin Belousov jz \vec_name\()_u 220*bd50262fSKonstantin Belousov PTI_UENTRY has_err=0 221*bd50262fSKonstantin Belousov jmp \vec_name\()_u 222*bd50262fSKonstantin Belousov .endm 223*bd50262fSKonstantin Belousov 224*bd50262fSKonstantin Belousov .macro INTR_PUSH_FRAME vec_name 225*bd50262fSKonstantin Belousov SUPERALIGN_TEXT 226*bd50262fSKonstantin Belousov .globl X\vec_name 227*bd50262fSKonstantin Belousov .type X\vec_name,@function 228*bd50262fSKonstantin Belousov X\vec_name: 229*bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* come from kernel? */ 230*bd50262fSKonstantin Belousov jz \vec_name\()_u /* Yes, dont swapgs again */ 231*bd50262fSKonstantin Belousov swapgs 232*bd50262fSKonstantin Belousov \vec_name\()_u: 233*bd50262fSKonstantin Belousov subq $TF_RIP,%rsp /* skip dummy tf_err and tf_trapno */ 234*bd50262fSKonstantin Belousov movq %rdi,TF_RDI(%rsp) 235*bd50262fSKonstantin Belousov movq %rsi,TF_RSI(%rsp) 236*bd50262fSKonstantin Belousov movq %rdx,TF_RDX(%rsp) 237*bd50262fSKonstantin Belousov movq %rcx,TF_RCX(%rsp) 238*bd50262fSKonstantin Belousov movq %r8,TF_R8(%rsp) 239*bd50262fSKonstantin Belousov movq %r9,TF_R9(%rsp) 240*bd50262fSKonstantin Belousov movq %rax,TF_RAX(%rsp) 241*bd50262fSKonstantin Belousov movq %rbx,TF_RBX(%rsp) 242*bd50262fSKonstantin Belousov movq %rbp,TF_RBP(%rsp) 243*bd50262fSKonstantin Belousov movq %r10,TF_R10(%rsp) 244*bd50262fSKonstantin Belousov movq %r11,TF_R11(%rsp) 245*bd50262fSKonstantin Belousov movq %r12,TF_R12(%rsp) 246*bd50262fSKonstantin Belousov movq %r13,TF_R13(%rsp) 247*bd50262fSKonstantin Belousov movq %r14,TF_R14(%rsp) 248*bd50262fSKonstantin Belousov movq %r15,TF_R15(%rsp) 249*bd50262fSKonstantin Belousov SAVE_SEGS 250*bd50262fSKonstantin Belousov movl $TF_HASSEGS,TF_FLAGS(%rsp) 251*bd50262fSKonstantin Belousov cld 252*bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,TF_CS(%rsp) /* come from kernel ? */ 253*bd50262fSKonstantin Belousov jz 1f /* yes, leave PCB_FULL_IRET alone */ 254*bd50262fSKonstantin Belousov movq PCPU(CURPCB),%r8 255*bd50262fSKonstantin Belousov andl $~PCB_FULL_IRET,PCB_FLAGS(%r8) 256*bd50262fSKonstantin Belousov 1: 257*bd50262fSKonstantin Belousov .endm 258*bd50262fSKonstantin Belousov 259*bd50262fSKonstantin Belousov .macro INTR_HANDLER vec_name 260*bd50262fSKonstantin Belousov .text 261*bd50262fSKonstantin Belousov PTI_INTRENTRY \vec_name 262*bd50262fSKonstantin Belousov INTR_PUSH_FRAME \vec_name 263*bd50262fSKonstantin Belousov .endm 264*bd50262fSKonstantin Belousov 265*bd50262fSKonstantin Belousov .macro RESTORE_REGS 266*bd50262fSKonstantin Belousov movq TF_RDI(%rsp),%rdi 267*bd50262fSKonstantin Belousov movq TF_RSI(%rsp),%rsi 268*bd50262fSKonstantin Belousov movq TF_RDX(%rsp),%rdx 269*bd50262fSKonstantin Belousov movq TF_RCX(%rsp),%rcx 270*bd50262fSKonstantin Belousov movq TF_R8(%rsp),%r8 271*bd50262fSKonstantin Belousov movq TF_R9(%rsp),%r9 272*bd50262fSKonstantin Belousov movq TF_RAX(%rsp),%rax 273*bd50262fSKonstantin Belousov movq TF_RBX(%rsp),%rbx 274*bd50262fSKonstantin Belousov movq TF_RBP(%rsp),%rbp 275*bd50262fSKonstantin Belousov movq TF_R10(%rsp),%r10 276*bd50262fSKonstantin Belousov movq TF_R11(%rsp),%r11 277*bd50262fSKonstantin Belousov movq TF_R12(%rsp),%r12 278*bd50262fSKonstantin Belousov movq TF_R13(%rsp),%r13 279*bd50262fSKonstantin Belousov movq TF_R14(%rsp),%r14 280*bd50262fSKonstantin Belousov movq TF_R15(%rsp),%r15 281*bd50262fSKonstantin Belousov .endm 282*bd50262fSKonstantin Belousov 2830d2a2989SPeter Wemm #endif /* LOCORE */ 2840d2a2989SPeter Wemm 2851a9cdd37SRoger Pau Monné #ifdef __STDC__ 2861a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata...) \ 2871a9cdd37SRoger Pau Monné .pushsection .note.name ; \ 2881a9cdd37SRoger Pau Monné .align 4 ; \ 2891a9cdd37SRoger Pau Monné .long 2f - 1f /* namesz */ ; \ 2901a9cdd37SRoger Pau Monné .long 4f - 3f /* descsz */ ; \ 2911a9cdd37SRoger Pau Monné .long type ; \ 2921a9cdd37SRoger Pau Monné 1:.asciz #name ; \ 2931a9cdd37SRoger Pau Monné 2:.align 4 ; \ 2941a9cdd37SRoger Pau Monné 3:desctype descdata ; \ 2951a9cdd37SRoger Pau Monné 4:.align 4 ; \ 2961a9cdd37SRoger Pau Monné .popsection 2971a9cdd37SRoger Pau Monné #else /* !__STDC__, i.e. -traditional */ 2981a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata) \ 2991a9cdd37SRoger Pau Monné .pushsection .note.name ; \ 3001a9cdd37SRoger Pau Monné .align 4 ; \ 3011a9cdd37SRoger Pau Monné .long 2f - 1f /* namesz */ ; \ 3021a9cdd37SRoger Pau Monné .long 4f - 3f /* descsz */ ; \ 3031a9cdd37SRoger Pau Monné .long type ; \ 3041a9cdd37SRoger Pau Monné 1:.asciz "name" ; \ 3051a9cdd37SRoger Pau Monné 2:.align 4 ; \ 3061a9cdd37SRoger Pau Monné 3:desctype descdata ; \ 3071a9cdd37SRoger Pau Monné 4:.align 4 ; \ 3081a9cdd37SRoger Pau Monné .popsection 3091a9cdd37SRoger Pau Monné #endif /* __STDC__ */ 3101a9cdd37SRoger Pau Monné 311ab9678acSBruce Evans #endif /* !_MACHINE_ASMACROS_H_ */ 312