1bd50262fSKonstantin 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 * 8bd50262fSKonstantin Belousov * Copyright (c) 2018 The FreeBSD Foundation 9bd50262fSKonstantin Belousov * All rights reserved. 10bd50262fSKonstantin Belousov * 11bd50262fSKonstantin Belousov * Portions of this software were developed by 12bd50262fSKonstantin Belousov * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from 13bd50262fSKonstantin Belousov * the FreeBSD Foundation. 14bd50262fSKonstantin 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 164bd50262fSKonstantin Belousov /* 165bd50262fSKonstantin Belousov * Convenience macro for declaring interrupt entry points. 166bd50262fSKonstantin Belousov */ 167bd50262fSKonstantin Belousov #define IDTVEC(name) ALIGN_TEXT; .globl __CONCAT(X,name); \ 168bd50262fSKonstantin Belousov .type __CONCAT(X,name),@function; __CONCAT(X,name): 169bd50262fSKonstantin Belousov 170bd50262fSKonstantin Belousov .macro SAVE_SEGS 171bd50262fSKonstantin Belousov movw %fs,TF_FS(%rsp) 172bd50262fSKonstantin Belousov movw %gs,TF_GS(%rsp) 173bd50262fSKonstantin Belousov movw %es,TF_ES(%rsp) 174bd50262fSKonstantin Belousov movw %ds,TF_DS(%rsp) 175bd50262fSKonstantin Belousov .endm 176bd50262fSKonstantin Belousov 177bd50262fSKonstantin Belousov .macro MOVE_STACKS qw 178*13cad9afSKonstantin Belousov .L.offset=0 179bd50262fSKonstantin Belousov .rept \qw 180*13cad9afSKonstantin Belousov movq .L.offset(%rsp),%rdx 181*13cad9afSKonstantin Belousov movq %rdx,.L.offset(%rax) 182*13cad9afSKonstantin Belousov .L.offset=.L.offset+8 183bd50262fSKonstantin Belousov .endr 184bd50262fSKonstantin Belousov .endm 185bd50262fSKonstantin Belousov 186b4dfc9d7SKonstantin Belousov .macro PTI_UUENTRY has_err 187bd50262fSKonstantin Belousov movq PCPU(KCR3),%rax 188bd50262fSKonstantin Belousov movq %rax,%cr3 189bd50262fSKonstantin Belousov movq PCPU(RSP0),%rax 190bd50262fSKonstantin Belousov subq $PTI_SIZE,%rax 191406bc0daSKonstantin Belousov MOVE_STACKS ((PTI_SIZE / 8) - 1 + \has_err) 192bd50262fSKonstantin Belousov movq %rax,%rsp 193bd50262fSKonstantin Belousov popq %rdx 194bd50262fSKonstantin Belousov popq %rax 195bd50262fSKonstantin Belousov .endm 196bd50262fSKonstantin Belousov 197b4dfc9d7SKonstantin Belousov .macro PTI_UENTRY has_err 198b4dfc9d7SKonstantin Belousov swapgs 199b4dfc9d7SKonstantin Belousov pushq %rax 200b4dfc9d7SKonstantin Belousov pushq %rdx 201b4dfc9d7SKonstantin Belousov PTI_UUENTRY \has_err 202b4dfc9d7SKonstantin Belousov .endm 203b4dfc9d7SKonstantin Belousov 204bd50262fSKonstantin Belousov .macro PTI_ENTRY name, cont, has_err=0 205bd50262fSKonstantin Belousov ALIGN_TEXT 206bd50262fSKonstantin Belousov .globl X\name\()_pti 207bd50262fSKonstantin Belousov .type X\name\()_pti,@function 208bd50262fSKonstantin Belousov X\name\()_pti: 209bd50262fSKonstantin Belousov /* %rax, %rdx and possibly err not yet pushed */ 210bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-(2+1-\has_err)*8(%rsp) 211bd50262fSKonstantin Belousov jz \cont 212bd50262fSKonstantin Belousov PTI_UENTRY \has_err 213bd50262fSKonstantin Belousov swapgs 214bd50262fSKonstantin Belousov jmp \cont 215bd50262fSKonstantin Belousov .endm 216bd50262fSKonstantin Belousov 217bd50262fSKonstantin Belousov .macro PTI_INTRENTRY vec_name 218bd50262fSKonstantin Belousov SUPERALIGN_TEXT 219bd50262fSKonstantin Belousov .globl X\vec_name\()_pti 220bd50262fSKonstantin Belousov .type X\vec_name\()_pti,@function 221bd50262fSKonstantin Belousov X\vec_name\()_pti: 222bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* err, %rax, %rdx not pushed */ 223bd50262fSKonstantin Belousov jz \vec_name\()_u 224bd50262fSKonstantin Belousov PTI_UENTRY has_err=0 225bd50262fSKonstantin Belousov jmp \vec_name\()_u 226bd50262fSKonstantin Belousov .endm 227bd50262fSKonstantin Belousov 228bd50262fSKonstantin Belousov .macro INTR_PUSH_FRAME vec_name 229bd50262fSKonstantin Belousov SUPERALIGN_TEXT 230bd50262fSKonstantin Belousov .globl X\vec_name 231bd50262fSKonstantin Belousov .type X\vec_name,@function 232bd50262fSKonstantin Belousov X\vec_name: 233bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,PTI_CS-3*8(%rsp) /* come from kernel? */ 234bd50262fSKonstantin Belousov jz \vec_name\()_u /* Yes, dont swapgs again */ 235bd50262fSKonstantin Belousov swapgs 236bd50262fSKonstantin Belousov \vec_name\()_u: 237bd50262fSKonstantin Belousov subq $TF_RIP,%rsp /* skip dummy tf_err and tf_trapno */ 238bd50262fSKonstantin Belousov movq %rdi,TF_RDI(%rsp) 239bd50262fSKonstantin Belousov movq %rsi,TF_RSI(%rsp) 240bd50262fSKonstantin Belousov movq %rdx,TF_RDX(%rsp) 241bd50262fSKonstantin Belousov movq %rcx,TF_RCX(%rsp) 242bd50262fSKonstantin Belousov movq %r8,TF_R8(%rsp) 243bd50262fSKonstantin Belousov movq %r9,TF_R9(%rsp) 244bd50262fSKonstantin Belousov movq %rax,TF_RAX(%rsp) 245bd50262fSKonstantin Belousov movq %rbx,TF_RBX(%rsp) 246bd50262fSKonstantin Belousov movq %rbp,TF_RBP(%rsp) 247bd50262fSKonstantin Belousov movq %r10,TF_R10(%rsp) 248bd50262fSKonstantin Belousov movq %r11,TF_R11(%rsp) 249bd50262fSKonstantin Belousov movq %r12,TF_R12(%rsp) 250bd50262fSKonstantin Belousov movq %r13,TF_R13(%rsp) 251bd50262fSKonstantin Belousov movq %r14,TF_R14(%rsp) 252bd50262fSKonstantin Belousov movq %r15,TF_R15(%rsp) 253bd50262fSKonstantin Belousov SAVE_SEGS 254bd50262fSKonstantin Belousov movl $TF_HASSEGS,TF_FLAGS(%rsp) 255bd50262fSKonstantin Belousov cld 256bd50262fSKonstantin Belousov testb $SEL_RPL_MASK,TF_CS(%rsp) /* come from kernel ? */ 257bd50262fSKonstantin Belousov jz 1f /* yes, leave PCB_FULL_IRET alone */ 258bd50262fSKonstantin Belousov movq PCPU(CURPCB),%r8 259bd50262fSKonstantin Belousov andl $~PCB_FULL_IRET,PCB_FLAGS(%r8) 260bd50262fSKonstantin Belousov 1: 261bd50262fSKonstantin Belousov .endm 262bd50262fSKonstantin Belousov 263bd50262fSKonstantin Belousov .macro INTR_HANDLER vec_name 264bd50262fSKonstantin Belousov .text 265bd50262fSKonstantin Belousov PTI_INTRENTRY \vec_name 266bd50262fSKonstantin Belousov INTR_PUSH_FRAME \vec_name 267bd50262fSKonstantin Belousov .endm 268bd50262fSKonstantin Belousov 269bd50262fSKonstantin Belousov .macro RESTORE_REGS 270bd50262fSKonstantin Belousov movq TF_RDI(%rsp),%rdi 271bd50262fSKonstantin Belousov movq TF_RSI(%rsp),%rsi 272bd50262fSKonstantin Belousov movq TF_RDX(%rsp),%rdx 273bd50262fSKonstantin Belousov movq TF_RCX(%rsp),%rcx 274bd50262fSKonstantin Belousov movq TF_R8(%rsp),%r8 275bd50262fSKonstantin Belousov movq TF_R9(%rsp),%r9 276bd50262fSKonstantin Belousov movq TF_RAX(%rsp),%rax 277bd50262fSKonstantin Belousov movq TF_RBX(%rsp),%rbx 278bd50262fSKonstantin Belousov movq TF_RBP(%rsp),%rbp 279bd50262fSKonstantin Belousov movq TF_R10(%rsp),%r10 280bd50262fSKonstantin Belousov movq TF_R11(%rsp),%r11 281bd50262fSKonstantin Belousov movq TF_R12(%rsp),%r12 282bd50262fSKonstantin Belousov movq TF_R13(%rsp),%r13 283bd50262fSKonstantin Belousov movq TF_R14(%rsp),%r14 284bd50262fSKonstantin Belousov movq TF_R15(%rsp),%r15 285bd50262fSKonstantin Belousov .endm 286bd50262fSKonstantin Belousov 2870d2a2989SPeter Wemm #endif /* LOCORE */ 2880d2a2989SPeter Wemm 2891a9cdd37SRoger Pau Monné #ifdef __STDC__ 2901a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata...) \ 2911a9cdd37SRoger Pau Monné .pushsection .note.name ; \ 2921a9cdd37SRoger Pau Monné .align 4 ; \ 2931a9cdd37SRoger Pau Monné .long 2f - 1f /* namesz */ ; \ 2941a9cdd37SRoger Pau Monné .long 4f - 3f /* descsz */ ; \ 2951a9cdd37SRoger Pau Monné .long type ; \ 2961a9cdd37SRoger Pau Monné 1:.asciz #name ; \ 2971a9cdd37SRoger Pau Monné 2:.align 4 ; \ 2981a9cdd37SRoger Pau Monné 3:desctype descdata ; \ 2991a9cdd37SRoger Pau Monné 4:.align 4 ; \ 3001a9cdd37SRoger Pau Monné .popsection 3011a9cdd37SRoger Pau Monné #else /* !__STDC__, i.e. -traditional */ 3021a9cdd37SRoger Pau Monné #define ELFNOTE(name, type, desctype, descdata) \ 3031a9cdd37SRoger Pau Monné .pushsection .note.name ; \ 3041a9cdd37SRoger Pau Monné .align 4 ; \ 3051a9cdd37SRoger Pau Monné .long 2f - 1f /* namesz */ ; \ 3061a9cdd37SRoger Pau Monné .long 4f - 3f /* descsz */ ; \ 3071a9cdd37SRoger Pau Monné .long type ; \ 3081a9cdd37SRoger Pau Monné 1:.asciz "name" ; \ 3091a9cdd37SRoger Pau Monné 2:.align 4 ; \ 3101a9cdd37SRoger Pau Monné 3:desctype descdata ; \ 3111a9cdd37SRoger Pau Monné 4:.align 4 ; \ 3121a9cdd37SRoger Pau Monné .popsection 3131a9cdd37SRoger Pau Monné #endif /* __STDC__ */ 3141a9cdd37SRoger Pau Monné 315ab9678acSBruce Evans #endif /* !_MACHINE_ASMACROS_H_ */ 316