summaryrefslogtreecommitdiff
path: root/sys/arch/arm64/stand/efiboot/exec.c
blob: d478c44716df263d0412435f9ee73fdd21dbb18c (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
/*	$OpenBSD: exec.c,v 1.8 2020/05/10 11:55:42 kettenis Exp $	*/

/*
 * Copyright (c) 2006, 2016 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/reboot.h>
#include <dev/cons.h>

#include <lib/libkern/libkern.h>
#include <lib/libsa/loadfile.h>
#include <sys/exec_elf.h>

#include <efi.h>
#include <stand/boot/cmd.h>

#include <machine/armreg.h>

#include "efiboot.h"
#include "libsa.h"
#include "fdt.h"

typedef void (*startfuncp)(void *, void *, void *) __attribute__ ((noreturn));

unsigned int cpu_get_dcache_line_size(void);
void cpu_flush_dcache(vaddr_t, vsize_t);
void cpu_inval_icache(void);

unsigned int
cpu_get_dcache_line_size(void)
{
	uint64_t ctr;
	unsigned int dcl_size;

	/* Accessible from all security levels */
	ctr = READ_SPECIALREG(ctr_el0);

	/*
	 * Relevant field [19:16] is LOG2
	 * of the number of words in DCache line
	 */
	dcl_size = CTR_DLINE_SIZE(ctr);

	/* Size of word shifted by cache line size */
	return (sizeof(int) << dcl_size);
}

void
cpu_flush_dcache(vaddr_t addr, vsize_t len)
{
	uint64_t cl_size;
	vaddr_t end;

	cl_size = cpu_get_dcache_line_size();

	/* Calculate end address to clean */
	end = addr + len;
	/* Align start address to cache line */
	addr = addr & ~(cl_size - 1);

	for (; addr < end; addr += cl_size)
		__asm volatile("dc civac, %0" :: "r" (addr) : "memory");

	/* Full system DSB */
	__asm volatile("dsb sy" ::: "memory");
}

void
cpu_inval_icache(void)
{
	__asm volatile(
	    "ic		ialluis	\n"
	    "dsb	ish	\n"
	    : : : "memory");
}

void
run_loadfile(uint64_t *marks, int howto)
{
	char args[256];
	char *cp;
	void *fdt;

	strlcpy(args, cmd.path, sizeof(args));
	cp = args + strlen(args);

	*cp++ = ' ';
	*cp = '-';
	if (howto & RB_ASKNAME)
		*++cp = 'a';
	if (howto & RB_CONFIG)
		*++cp = 'c';
	if (howto & RB_SINGLE)
		*++cp = 's';
	if (howto & RB_KDB)
		*++cp = 'd';
	if (*cp == '-')
		*--cp = 0;
	else
		*++cp = 0;

	fdt = efi_makebootargs(args, howto);

	efi_cleanup();

	cpu_flush_dcache(marks[MARK_ENTRY], marks[MARK_END] - marks[MARK_ENTRY]);
	cpu_inval_icache();

	cpu_flush_dcache((vaddr_t)fdt, fdt_get_size(fdt));

	(*(startfuncp)(marks[MARK_ENTRY]))((void *)marks[MARK_END], 0, fdt);

	/* NOTREACHED */
}