xref: /freebsd/stand/kboot/kboot/arch/powerpc64/ppc64_elf_freebsd.c (revision 6aea7b224a34ccc800f9598f034838af7e47eb62)
1 /*-
2  * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #define __ELF_WORD_SIZE 64
28 
29 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/linker.h>
32 
33 #include <machine/metadata.h>
34 #include <machine/elf.h>
35 
36 #include <stand.h>
37 
38 #include "bootstrap.h"
39 #include "syscall_nr.h"
40 #include "host_syscall.h"
41 #include "modinfo.h"
42 #include "kboot.h"
43 
44 extern char		end[];
45 extern void		*kerneltramp;
46 extern size_t		szkerneltramp;
47 
48 struct trampoline_data {
49 	uint32_t	kernel_entry;
50 	uint32_t	dtb;
51 	uint32_t	phys_mem_offset;
52 	uint32_t	of_entry;
53 	uint32_t	mdp;
54 	uint32_t	mdp_size;
55 };
56 
57 int
ppc64_elf_loadfile(char * filename,uint64_t dest,struct preloaded_file ** result)58 ppc64_elf_loadfile(char *filename, uint64_t dest,
59     struct preloaded_file **result)
60 {
61 	int	r;
62 
63 	r = __elfN(loadfile)(filename, dest, result);
64 	if (r != 0)
65 		return (r);
66 
67 	return (0);
68 }
69 
70 int
ppc64_elf_exec(struct preloaded_file * fp)71 ppc64_elf_exec(struct preloaded_file *fp)
72 {
73 	struct file_metadata	*fmp;
74 	vm_offset_t		mdp, dtb;
75 	Elf_Ehdr		*e;
76 	int			error;
77 	uint32_t		*trampoline;
78 	uint64_t		entry;
79 	uint64_t		trampolinebase;
80 	struct trampoline_data	*trampoline_data;
81 	int			nseg;
82 	void			*kseg;
83 
84 	if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
85 		return(EFTYPE);
86 	}
87 	e = (Elf_Ehdr *)&fmp->md_data;
88 
89 	/*
90 	 * Figure out where to put it.
91 	 *
92 	 * Linux does not allow us to do kexec_load into any part of memory. Ask
93 	 * kboot_get_phys_load_segment to resolve the first available chunk of
94 	 * physical memory where loading is possible (load_addr).
95 	 *
96 	 * Memory organization is shown below.  It is assumed, that text segment
97 	 * offset of kernel ELF (KERNPHYSADDR) is non-zero, which is true for
98 	 * PPC/PPC64 architectures, where default is 0x100000.
99 	 *
100 	 * load_addr:                 trampoline code
101 	 * load_addr + KERNPHYSADDR:  kernel text segment
102 	 */
103 	trampolinebase = kboot_get_phys_load_segment();
104 	printf("Load address at %#jx\n", (uintmax_t)trampolinebase);
105 	printf("Relocation offset is %#jx\n", (uintmax_t)elf64_relocation_offset);
106 
107 	/* Set up loader trampoline */
108 	trampoline = malloc(szkerneltramp);
109 	memcpy(trampoline, &kerneltramp, szkerneltramp);
110 
111 	/* Parse function descriptor for ELFv1 kernels */
112 	if ((e->e_flags & 3) == 2)
113 		entry = e->e_entry;
114 	else {
115 		archsw.arch_copyout(e->e_entry + elf64_relocation_offset,
116 		    &entry, 8);
117 		entry = be64toh(entry);
118 	}
119 
120 	/*
121 	 * Placeholder for trampoline data is at trampolinebase + 0x08
122 	 * CAUTION: all data must be Big Endian
123 	 */
124 	trampoline_data = (void*)&trampoline[2];
125 	trampoline_data->kernel_entry = htobe32(entry + elf64_relocation_offset);
126 	trampoline_data->phys_mem_offset = htobe32(0);
127 	trampoline_data->of_entry = htobe32(0);
128 
129 	if ((error = md_load64(fp->f_args, &mdp, &dtb)) != 0)
130 		return (error);
131 
132 	trampoline_data->dtb = htobe32(dtb);
133 	trampoline_data->mdp = htobe32(mdp);
134 	trampoline_data->mdp_size = htobe32(0xfb5d104d);
135 
136 	printf("Kernel entry at %#jx (%#x) ...\n",
137 	    entry, be32toh(trampoline_data->kernel_entry));
138 	printf("DTB at %#x, mdp at %#x\n",
139 	    be32toh(trampoline_data->dtb), be32toh(trampoline_data->mdp));
140 
141 	dev_cleanup();
142 
143 	archsw.arch_copyin(trampoline, trampolinebase, szkerneltramp);
144 	free(trampoline);
145 
146 	kboot_kseg_get(&nseg, &kseg);
147 
148 	error = host_kexec_load(trampolinebase, nseg, kseg, HOST_KEXEC_ARCH_PPC64);
149 	if (error != 0)
150 		panic("kexec_load returned error: %d", error);
151 
152 	error = host_reboot(HOST_REBOOT_MAGIC1, HOST_REBOOT_MAGIC2, HOST_REBOOT_CMD_KEXEC,
153 	    (uintptr_t)NULL);
154 	if (error != 0)
155 		panic("reboot returned error: %d", error);
156 
157 	while (1) {}
158 }
159 
160 struct file_format	ppc_elf64 =
161 {
162 	ppc64_elf_loadfile,
163 	ppc64_elf_exec
164 };
165 
166 /*
167  * Sort formats so that those that can detect based on arguments rather than
168  * reading the file first.
169  */
170 
171 struct file_format *file_formats[] = {
172     &ppc_elf64,
173     NULL
174 };
175