xref: /openbsd-src/sys/arch/loongson/stand/boot/exec.c (revision 4b4220192dd9fe1a6144b269eae17bd145dc586f)
1*4b422019Smiod /*	$OpenBSD: exec.c,v 1.5 2022/08/10 12:20:05 miod Exp $	*/
2e923757cSmiod 
3e923757cSmiod /*
4e923757cSmiod  * Copyright (c) 2010 Miodrag Vallat.
5e923757cSmiod  *
6e923757cSmiod  * Permission to use, copy, modify, and distribute this software for any
7e923757cSmiod  * purpose with or without fee is hereby granted, provided that the above
8e923757cSmiod  * copyright notice and this permission notice appear in all copies.
9e923757cSmiod  *
10e923757cSmiod  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11e923757cSmiod  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12e923757cSmiod  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13e923757cSmiod  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14e923757cSmiod  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15e923757cSmiod  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16e923757cSmiod  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17e923757cSmiod  */
18e923757cSmiod 
19e923757cSmiod #include <sys/param.h>
20e923757cSmiod #include <sys/reboot.h>
21e923757cSmiod #include <machine/cpu.h>
22e923757cSmiod #include <machine/pmon.h>
23e923757cSmiod #include "libsa.h"
24e923757cSmiod #include <lib/libsa/loadfile.h>
25e923757cSmiod 
26e923757cSmiod typedef void (*program)(int32_t, int32_t, int32_t *, int32_t, uint64_t *);
27e923757cSmiod 
28e923757cSmiod #define	PTR_TO_CKSEG1(ptr)	(int32_t)(CKSEG1_BASE | (uint64_t)(ptr))
29e923757cSmiod 
30e923757cSmiod void
run_loadfile(uint64_t * marks,int howto)312340cfa5Sderaadt run_loadfile(uint64_t *marks, int howto)
32e923757cSmiod {
33e923757cSmiod 	int32_t newargc;
34e923757cSmiod 	int32_t *newargv;
35e923757cSmiod 	char kernelflags[8];
36e923757cSmiod 	char *c;
37e923757cSmiod 	const char *arg;
38e923757cSmiod 
39e923757cSmiod 	/*
40e923757cSmiod 	 * Build a new commandline:
41e923757cSmiod 	 * boot <device kernel is loaded from> -<kernel options>
42e923757cSmiod 	 */
43e923757cSmiod 
44e923757cSmiod 	newargc = howto == 0 ? 2 : 3;
45e923757cSmiod 	newargv = alloc(newargc * sizeof(int32_t));
46e923757cSmiod 	if (newargv == NULL)
47e923757cSmiod 		panic("out of memory");
48e923757cSmiod 
49e923757cSmiod 	arg = "boot";	/* kernel needs this. */
50e923757cSmiod 	newargv[0] = PTR_TO_CKSEG1(arg);
51e923757cSmiod 	newargv[1] = PTR_TO_CKSEG1(&pmon_bootdev);
52e923757cSmiod 	if (howto != 0) {
53e923757cSmiod 		c = kernelflags;
54e923757cSmiod 		*c++ = '-';
55e923757cSmiod 		if (howto & RB_ASKNAME)
56e923757cSmiod 			*c++ = 'a';
57e923757cSmiod 		if (howto & RB_CONFIG)
58e923757cSmiod 			*c++ = 'c';
59e923757cSmiod 		if (howto & RB_KDB)
60e923757cSmiod 			*c++ = 'd';
61*4b422019Smiod 		if (howto & RB_GOODRANDOM)
62*4b422019Smiod 			*c++ = 'g';
63e923757cSmiod 		if (howto & RB_SINGLE)
64e923757cSmiod 			*c++ = 's';
65e923757cSmiod 		*c = '\0';
66e923757cSmiod 		newargv[2] = PTR_TO_CKSEG1(&kernelflags);
67e923757cSmiod 	}
68e923757cSmiod 
69e923757cSmiod 	pmon_cacheflush();
70e923757cSmiod 
71e923757cSmiod 	(*(program)(marks[MARK_ENTRY]))(newargc, PTR_TO_CKSEG1(newargv),
72e923757cSmiod 	    pmon_envp, pmon_callvec,
73e923757cSmiod 	    (uint64_t *)PHYS_TO_CKSEG0(marks[MARK_END]));
74e923757cSmiod 
753c1af18dSmiod 	rd_invalidate();
76e923757cSmiod 	_rtt();
77e923757cSmiod }
78