xref: /freebsd-13.1/stand/mips/beri/loader/exec.c (revision d3e1307b)
1 /*-
2  * Copyright (c) 2013-2014 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
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 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 
37 #include <machine/bootinfo.h>
38 #include <machine/elf.h>
39 
40 #include <bootstrap.h>
41 #include <loader.h>
42 #include <mips.h>
43 #include <stand.h>
44 
45 static int	beri_elf64_loadfile(char *, uint64_t,
46 		    struct preloaded_file **);
47 static int	beri_elf64_exec(struct preloaded_file *fp);
48 
49 struct file_format beri_elf = {
50 	.l_load = beri_elf64_loadfile,
51 	.l_exec = beri_elf64_exec,
52 };
53 
54 /*
55  * bootinfo that we will pass onto the kernel; some fields derived from
56  * *boot2_bootinfop, others filled in by loader.
57  */
58 struct bootinfo	bootinfo;
59 
60 static int
beri_elf64_loadfile(char * filename,uint64_t dest,struct preloaded_file ** result)61 beri_elf64_loadfile(char *filename, uint64_t dest,
62     struct preloaded_file **result)
63 {
64 
65 	/*
66 	 * Some platforms require invalidation of instruction caches here; we
67 	 * don't need that currently.
68 	 */
69 	return (__elfN(loadfile)(filename, dest, result));
70 }
71 
72 static int
beri_elf64_exec(struct preloaded_file * fp)73 beri_elf64_exec(struct preloaded_file *fp)
74 {
75 	void (*entry)(register_t, register_t, register_t, register_t);
76 	struct file_metadata *md;
77 	vm_offset_t mdp;
78 	Elf_Ehdr *ehdr;
79 	int error;
80 
81 	md = file_findmetadata(fp, MODINFOMD_ELFHDR);
82 	if (md == NULL) {
83 		printf("%s: file_findmetadata failed\n", fp->f_name);
84 		return (EFTYPE);
85 	}
86 	ehdr = (Elf_Ehdr *)md->md_data;
87 
88 	error = md_load64(fp->f_args, &mdp, NULL);
89 	if (error) {
90 		printf("%s: md_load64 failed\n", fp->f_name);
91 		return (error);
92 	}
93 
94 	entry = (void *)ehdr->e_entry;
95 	printf("Kernel entry at %p\n", entry);
96 
97 	dev_cleanup();		/* XXXRW: Required? */
98 	printf("Kernel args: %s\n", fp->f_args);
99 
100 	/*
101 	 * Configure bootinfo for the loaded kernel.  Some values are
102 	 * inherited from the bootinfo passed to us by boot2 (e.g., DTB
103 	 * pointer); others are local to the loader (e.g., kernel boot flags).
104 	 */
105 	bzero(&bootinfo, sizeof(bootinfo));
106 	bootinfo.bi_version = BOOTINFO_VERSION;
107 	bootinfo.bi_size = sizeof(bootinfo);
108 	bootinfo.bi_boot2opts = boot2_bootinfo.bi_boot2opts;
109 	/* NB: bi_kernelname used only by boot2. */
110 	/* NB: bi_nfs_diskless not yet. */
111 	bootinfo.bi_dtb = boot2_bootinfo.bi_dtb;
112 	bootinfo.bi_memsize = boot2_bootinfo.bi_memsize;
113 	bootinfo.bi_modulep = mdp;
114 
115 	/*
116 	 * XXXRW: For now, pass 'memsize' rather than dtb or bootinfo.  This
117 	 * is the 'old' ABI spoken by Miniboot and the kernel.  To pass in
118 	 * boot2opts, modules, etc, we will need to fix this to pass in at
119 	 * least bootinfop.
120 	 */
121 	(*entry)(boot2_argc, (register_t)boot2_argv, (register_t)boot2_envv,
122 	    (register_t)&bootinfo);
123 
124 	panic("exec returned");
125 }
126