summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/armv7/exception_level.c
blob: 6648aed291e96b51b06d28fa925e370003df0ef0 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Switch to non-secure mode
 *
 * Copyright (c) 2018 Heinrich Schuchardt
 *
 * This module contains the ARMv7 specific code required for leaving the
 * secure mode before booting an operating system.
 */

#include <common.h>
#include <bootm.h>
#include <cpu_func.h>
#include <asm/armv7.h>
#include <asm/secure.h>
#include <asm/setjmp.h>

/**
 * entry_non_secure() - entry point when switching to non-secure mode
 *
 * When switching to non-secure mode switch_to_non_secure_mode() calls this
 * function passing a jump buffer. We use this jump buffer to restore the
 * original stack and register state.
 *
 * @non_secure_jmp:	jump buffer for restoring stack and registers
 */
static void entry_non_secure(struct jmp_buf_data *non_secure_jmp)
{
	dcache_enable();
	debug("Reached non-secure mode\n");

	/* Restore stack and registers saved in switch_to_non_secure_mode() */
	longjmp(non_secure_jmp, 1);
}

/**
 * switch_to_non_secure_mode() - switch to non-secure mode
 *
 * Operating systems may expect to run in non-secure mode. Here we check if
 * we are running in secure mode and switch to non-secure mode if necessary.
 */
void switch_to_non_secure_mode(void)
{
	static bool is_nonsec;
	struct jmp_buf_data non_secure_jmp;

	if (armv7_boot_nonsec() && !is_nonsec) {
		if (setjmp(&non_secure_jmp))
			return;
		dcache_disable();	/* flush cache before switch to HYP */
		armv7_init_nonsec();
		is_nonsec = true;
		secure_ram_addr(_do_nonsec_entry)(entry_non_secure,
						  (uintptr_t)&non_secure_jmp,
						  0, 0);
	}
}