xref: /freebsd/stand/powerpc/ofw/ppc64_elf_freebsd.c (revision 86077f4fd11070518a6d04eee7fdb93cbbfb1b52)
1e05bc4f9SNathan Whitehorn /*-
2e05bc4f9SNathan Whitehorn  * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org>
3e05bc4f9SNathan Whitehorn  * All rights reserved.
4e05bc4f9SNathan Whitehorn  *
5e05bc4f9SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
6e05bc4f9SNathan Whitehorn  * modification, are permitted provided that the following conditions
7e05bc4f9SNathan Whitehorn  * are met:
8e05bc4f9SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
9e05bc4f9SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
10e05bc4f9SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
11e05bc4f9SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
12e05bc4f9SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
13e05bc4f9SNathan Whitehorn  *
14e05bc4f9SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e05bc4f9SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e05bc4f9SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e05bc4f9SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e05bc4f9SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e05bc4f9SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e05bc4f9SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e05bc4f9SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e05bc4f9SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e05bc4f9SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e05bc4f9SNathan Whitehorn  * SUCH DAMAGE.
25e05bc4f9SNathan Whitehorn  */
26e05bc4f9SNathan Whitehorn 
27e05bc4f9SNathan Whitehorn #define __ELF_WORD_SIZE 64
28e05bc4f9SNathan Whitehorn 
29e05bc4f9SNathan Whitehorn #include <sys/param.h>
30e05bc4f9SNathan Whitehorn #include <sys/linker.h>
31e05bc4f9SNathan Whitehorn 
32e05bc4f9SNathan Whitehorn #include <machine/metadata.h>
33e05bc4f9SNathan Whitehorn #include <machine/elf.h>
34e05bc4f9SNathan Whitehorn #include <machine/md_var.h>
35e05bc4f9SNathan Whitehorn 
36e05bc4f9SNathan Whitehorn #include <stand.h>
37e05bc4f9SNathan Whitehorn 
38e05bc4f9SNathan Whitehorn #include "bootstrap.h"
39e05bc4f9SNathan Whitehorn #include "libofw.h"
40e05bc4f9SNathan Whitehorn #include "openfirm.h"
41335615c4SWarner Losh #include "modinfo.h"
42e05bc4f9SNathan Whitehorn 
43e05bc4f9SNathan Whitehorn extern char		end[];
44e05bc4f9SNathan Whitehorn extern vm_offset_t	reloc;	/* From <arch>/conf.c */
45e05bc4f9SNathan Whitehorn 
46e05bc4f9SNathan Whitehorn int
ppc64_ofw_elf_loadfile(char * filename,uint64_t dest,struct preloaded_file ** result)4756e53cb8SWarner Losh ppc64_ofw_elf_loadfile(char *filename, uint64_t dest,
48e05bc4f9SNathan Whitehorn     struct preloaded_file **result)
49e05bc4f9SNathan Whitehorn {
50e05bc4f9SNathan Whitehorn 	int	r;
51e05bc4f9SNathan Whitehorn 
52e05bc4f9SNathan Whitehorn 	r = __elfN(loadfile)(filename, dest, result);
53e05bc4f9SNathan Whitehorn 	if (r != 0)
54e05bc4f9SNathan Whitehorn 		return (r);
55e05bc4f9SNathan Whitehorn 
56e05bc4f9SNathan Whitehorn 	/*
57e05bc4f9SNathan Whitehorn 	 * No need to sync the icache for modules: this will
58e05bc4f9SNathan Whitehorn 	 * be done by the kernel after relocation.
59e05bc4f9SNathan Whitehorn 	 */
60*86077f4fSAhmad Khalifa 	if (!strcmp((*result)->f_type, md_kerntype))
61e05bc4f9SNathan Whitehorn 		__syncicache((void *) (*result)->f_addr, (*result)->f_size);
62e05bc4f9SNathan Whitehorn 	return (0);
63e05bc4f9SNathan Whitehorn }
64e05bc4f9SNathan Whitehorn 
65e05bc4f9SNathan Whitehorn int
ppc64_ofw_elf_exec(struct preloaded_file * fp)66e05bc4f9SNathan Whitehorn ppc64_ofw_elf_exec(struct preloaded_file *fp)
67e05bc4f9SNathan Whitehorn {
68e05bc4f9SNathan Whitehorn 	struct file_metadata	*fmp;
69e05bc4f9SNathan Whitehorn 	vm_offset_t		mdp, dtbp;
70e05bc4f9SNathan Whitehorn 	Elf_Ehdr		*e;
71e05bc4f9SNathan Whitehorn 	int			error;
72e05bc4f9SNathan Whitehorn 	intptr_t		entry;
73e05bc4f9SNathan Whitehorn 
74e05bc4f9SNathan Whitehorn 	if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
75e05bc4f9SNathan Whitehorn 		return(EFTYPE);
76e05bc4f9SNathan Whitehorn 	}
77e05bc4f9SNathan Whitehorn 	e = (Elf_Ehdr *)&fmp->md_data;
78e05bc4f9SNathan Whitehorn 
79e05bc4f9SNathan Whitehorn 	/* Handle function descriptor for ELFv1 kernels */
80e05bc4f9SNathan Whitehorn 	if ((e->e_flags & 3) == 2)
81e05bc4f9SNathan Whitehorn 		entry = e->e_entry;
82e05bc4f9SNathan Whitehorn 	else
83e05bc4f9SNathan Whitehorn 		entry = *(uint64_t *)(intptr_t)e->e_entry;
84e05bc4f9SNathan Whitehorn 
85e05bc4f9SNathan Whitehorn 	if ((error = md_load64(fp->f_args, &mdp, &dtbp)) != 0)
86e05bc4f9SNathan Whitehorn 		return (error);
87e05bc4f9SNathan Whitehorn 
88ff7449d6SLeandro Lupori 	printf("Kernel entry at 0x%x ...\n", entry);
89e05bc4f9SNathan Whitehorn 
90e05bc4f9SNathan Whitehorn 	dev_cleanup();
91e05bc4f9SNathan Whitehorn 
92e05bc4f9SNathan Whitehorn 	if (dtbp != 0) {
93e05bc4f9SNathan Whitehorn 		OF_quiesce();
94e05bc4f9SNathan Whitehorn 		((int (*)(u_long, u_long, u_long, void *, u_long))entry)(dtbp,
95e05bc4f9SNathan Whitehorn 		    0, 0, (void *)mdp, 0xfb5d104d);
96e05bc4f9SNathan Whitehorn 	} else {
97e05bc4f9SNathan Whitehorn 		OF_chain((void *)reloc, end - (char *)reloc, (void *)entry,
98e05bc4f9SNathan Whitehorn 		    (void *)mdp, 0xfb5d104d);
99e05bc4f9SNathan Whitehorn 	}
100e05bc4f9SNathan Whitehorn 
101e05bc4f9SNathan Whitehorn 	panic("exec returned");
102e05bc4f9SNathan Whitehorn }
103e05bc4f9SNathan Whitehorn 
104e05bc4f9SNathan Whitehorn struct file_format	ofw_elf64 =
105e05bc4f9SNathan Whitehorn {
106e05bc4f9SNathan Whitehorn 	ppc64_ofw_elf_loadfile,
107e05bc4f9SNathan Whitehorn 	ppc64_ofw_elf_exec
108e05bc4f9SNathan Whitehorn };
109