1/*-
2 * Copyright (c) 2013 The FreeBSD Foundation
3 * Copyright 2021 David Sebek <dasebek@gmail.com>
4 * All rights reserved.
5 *
6 * This software was developed by Benno Rice under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: stable/12/stand/efi/loader/arch/amd64/amd64_tramp.S 371476 2022-01-09 01:29:22Z kib $
31 */
32
33#include <machine/asmacros.h>
34
35	.text
36	.globl	amd64_tramp_inline
37
38/*
39 * void amd64_tramp_inline(uint64_t stack %rdi, uint64_t kernend %rsi,
40 *    uint64_t modulep %rdx, uint64_t pagetable %rcx, uint64_t entry %r8,
41 *    uint64_t copy_dst %r9, uint64_t copy_src 8(%rsp),
42 *    uint64_t copy_src_end 16(%rsp))
43 */
44amd64_tramp_inline:
45	cli			/* Make sure we don't get interrupted. */
46
47	/*
48	 * Copy the kernel from the staging area to the expected location
49	 * in memory. The following code is equivalent to the efi_copy_finish
50	 * function that amd64_tramp used to call. Inlining this code avoids
51	 * a scenario when the system froze because efi_copy_finish
52	 * overwrote its own code that just happened to be located somewhere
53	 * in the destination range.
54	 *
55	 * while (copy_src < copy_src_end) *copy_dst++ = *copy_src++;
56	 */
57	movq	8(%rsp), %rax	/* rax = copy_src */
58	movq	16(%rsp), %r10	/* r10 = copy_src_end */
59	cmpq	%r10, %rax
60	jnb	copy_done
61	subq	%rax, %r9	/* r9 = copy_dst - copy_src */
62loop:
63	movq	(%rax), %r11
64	movq	%r11, (%rax,%r9)
65	addq	$8, %rax
66	cmpq	%rax, %r10
67	ja	loop
68copy_done:
69
70	movq	%rdi,%rsp	/* Switch to our temporary stack. */
71
72	pushq	%rsi		/* Push kernend. */
73	salq	$32,%rdx	/* Shift modulep and push it. */
74	pushq	%rdx
75	pushq	%r8		/* Push the entry address. */
76	movq	%rcx,%cr3	/* Switch page tables. */
77	ret			/* "Return" to kernel entry. */
78
79	ALIGN_TEXT
80amd64_tramp_inline_end:
81
82	.data
83	.globl	amd64_tramp_inline_size
84amd64_tramp_inline_size:
85	.long	amd64_tramp_inline_end-amd64_tramp_inline
86