Skip to contentSkip to conversation

Add RISC-V TEE support

PATCH New liushiwei 6 participants 14 messages Jan 11, 2023 – Jan 24, 2023 RISC-V

lore
Share & tools
lore

liushiwei

author
#0 lore raw

PATCH 1/1 Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux, opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP-TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.
TEE function is off by default, when using configuration is added in the config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

---
 include/sbi/sbi_ecall_interface.h         |   3 +
 include/sbi_utils/tee/tee_context.h       | 104 ++++++++++
 include/sbi_utils/tee/tee_sm_dispatcher.h |  44 +++++
 include/sbi_utils/tee/teeecall_opteed.h   | 145 ++++++++++++++
 lib/sbi/Kconfig                           |   4 +
 lib/sbi/objects.mk                        |   3 +
 lib/sbi/sbi_ecall_tee.c                   |  19 ++
 lib/sbi/sbi_init.c                        |   5 +
 lib/utils/tee/objects.mk                  |  16 ++
 lib/utils/tee/tee_context.S               | 189 ++++++++++++++++++
 lib/utils/tee/tee_sm_dispatcher.c         | 228 ++++++++++++++++++++++
 11 files changed, 760 insertions(+)
 create mode 100644 include/sbi_utils/tee/tee_context.h
 create mode 100644 include/sbi_utils/tee/tee_sm_dispatcher.h
 create mode 100644 include/sbi_utils/tee/teeecall_opteed.h
 create mode 100644 lib/sbi/sbi_ecall_tee.c
 create mode 100644 lib/utils/tee/objects.mk
 create mode 100644 lib/utils/tee/tee_context.S
 create mode 100644 lib/utils/tee/tee_sm_dispatcher.c
include/sbi/sbi_ecall_interface.h +3 -0
index a3f2bf4..5ed5bd5 100644
@@ -238,6 +238,9 @@ enum sbi_pmu_ctr_type {
 #define SBI_EXT_VENDOR_END			0x09FFFFFF
 #define SBI_EXT_FIRMWARE_START			0x0A000000
 #define SBI_EXT_FIRMWARE_END			0x0AFFFFFF
+#define SBI_EXT_TEE_START			0x0A000000
+#define SBI_EXT_TEE_END				0x0AFFFFFF
+#define SBI_EXT_TEE				0xFFFFEEEE
 
 /* SBI return error codes */
 #define SBI_SUCCESS				0
include/sbi_utils/tee/tee_context.h new file +104 -0

104 lines not on this page · raw message

include/sbi_utils/tee/tee_sm_dispatcher.h new file +44 -0

44 lines not on this page · raw message

include/sbi_utils/tee/teeecall_opteed.h new file +145 -0

145 lines not on this page · raw message

lib/sbi/Kconfig +4 -0
index df74bba..7cfc8c3 100644
@@ -34,4 +34,8 @@ config SBI_ECALL_VENDOR
 	bool "Platform-defined vendor extensions"
 	default y
 
+config SBI_ECALL_TEE
+	bool "trusted execution environment"
+	default n
+
 endmenu
lib/sbi/objects.mk +3 -0
index c774ebb..ea79924 100644
@@ -43,6 +43,9 @@ libsbi-objs-$(CONFIG_SBI_ECALL_LEGACY) += sbi_ecall_legacy.o
 carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_VENDOR) += ecall_vendor
 libsbi-objs-$(CONFIG_SBI_ECALL_VENDOR) += sbi_ecall_vendor.o
 
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_TEE) += ecall_tee
+libsbi-objs-$(CONFIG_SBI_ECALL_TEE) += sbi_ecall_tee.o
+
 libsbi-objs-y += sbi_bitmap.o
 libsbi-objs-y += sbi_bitops.o
 libsbi-objs-y += sbi_console.o
lib/sbi/sbi_ecall_tee.c new file +19 -0
new file mode 100644
index 0000000..86deea3
@@ -0,0 +1,19 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright  2023  Beijing ESWIN Computing Technology Co., Ltd.
+ *
+ * Authors:
+ *   Chen Chaokai <[email protected]>
+ *   Liu Shiwei <[email protected]>
+ */
+
+#include <sbi/sbi_ecall.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi_utils/tee/tee_sm_dispatcher.h>
+
+struct sbi_ecall_extension ecall_tee = {
+	.extid_start = SBI_EXT_TEE,
+	.extid_end = SBI_EXT_TEE,
+	.handle = sbi_ecall_tee_handler,
+};
lib/sbi/sbi_init.c +5 -0
index 259a191..1158949 100644
@@ -26,6 +26,7 @@
 #include <sbi/sbi_timer.h>
 #include <sbi/sbi_tlb.h>
 #include <sbi/sbi_version.h>
+#include <sbi_utils/tee/tee_sm_dispatcher.h>
 
 #define BANNER                                              \
 	"   ____                    _____ ____ _____\n"     \
@@ -350,6 +351,10 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 	init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
 	(*init_count)++;
 
+#ifdef CONFIG_SBI_ECALL_TEE
+	tee_os_init();
+#endif
+
 	sbi_hsm_prepare_next_jump(scratch, hartid);
 	sbi_hart_switch_mode(hartid, scratch->next_arg1, scratch->next_addr,
 			     scratch->next_mode, false);
lib/utils/tee/objects.mk new file +16 -0
new file mode 100644
index 0000000..5bf81f7
@@ -0,0 +1,16 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright  2023  Beijing ESWIN Computing Technology Co., Ltd.
+#
+# Authors:
+#   Chen Chaokai <[email protected]>
+#   Liu Shiwei <[email protected]>
+#
+
+ifdef CONFIG_TEE_LOAD_ADDR
+firmware-cflags-y += -DTEE_LOAD_ADDR=$(CONFIG_TEE_LOAD_ADDR)
+endif
+
+libsbiutils-objs-$(CONFIG_SBI_ECALL_TEE) += tee/tee_context.o
+libsbiutils-objs-$(CONFIG_SBI_ECALL_TEE) += tee/tee_sm_dispatcher.o
lib/utils/tee/tee_context.S new file +189 -0

189 lines not on this page · raw message

lib/utils/tee/tee_sm_dispatcher.c new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 Add RISC-V TEE support

-----Original Message-----

Quoted 4 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 3 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 1 line

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

Signature · 3 lines
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
author
#2 lore raw

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just continues the idea of this workaround, and we may commit hardware-dependent code later.

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 3 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH 1/1 Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just continues the idea of this workaround, and we may commit hardware-dependent code later.

We can't blindly use SBI extension ID and function ID space for TEE.

Please share a draft proposal of how OP-TEE calls will be implemented as SBI calls.

I see that you have reserved an entire range of SBI extension IDs for OP-TEE. This is a waste of the SBI extension ID space.

Regards,
Anup

Quoted 48 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 3 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#4 lore raw

Hi, Anup
You mean this code?

+#define SBI_EXT_TEE_START                      0x0A000000
+#define SBI_EXT_TEE_END                        0x0AFFFFFF
+#define SBI_EXT_TEE                            0xFFFFEEEE

Current code is not in use SBI_EXT_TEE_START and SBI_EXT_TEE_END, I use SBI_EXT_TEE, I wrote these two macros in reference to
SBI_EXT_FIRMWARE_START, I'd like your advice on this part.
Currently, there are 20 scenarios, 12 of which are reserved for later use. I am not familiar with the rules for the use of SBI extension ID
and funciton ID. Because of the number of arguments, instead of using linux sbi_ecall, I encapsulated the ecall instruction by ourselves. I are not sure if this approach meets the requirements of opensbi and linux, So I made a redundant macro definition here. Could you give me some advice? Thank you.

Regards,
liushiwei

-----????-----
???: Anup Patel [mailto:apatel at ventanamicro.com]
????: 2023?1?11? 20:34
???: liushiwei <[email protected]>
??: hchauhan at ventanamicro.com; opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone
hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want?
The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

We can't blindly use SBI extension ID and function ID space for TEE.

Please share a draft proposal of how OP-TEE calls will be implemented as SBI calls.

I see that you have reserved an entire range of SBI extension IDs for OP-TEE. This is a waste of the SBI extension ID space.

Regards,
Anup

Quoted 49 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#5 lore raw
In reply to Anup Patel

Hi, Anup
I've combed through the linux code. What do you think of the following change?

arch/riscv/include/asm/sbi.h +1 -0
index d1c37479d..9696c8c77 100644
@@ -29,6 +29,7 @@ enum sbi_ext_id {
        SBI_EXT_RFENCE = 0x52464E43,
        SBI_EXT_HSM = 0x48534D,
        SBI_EXT_SRST = 0x53525354,
+       SBI_EXT_TEE = 0x544545,
 

I find that these values are just transformations of these letters?So I just use the ext id, not the func id? While the sbi_ecall_tee_handler function uses other registers, such as t0.

Regards,
Liushiwei

-----????-----
???: liushiwei [mailto:liushiwei at eswincomputing.com]
????: 2023?1?12? 15:06
???: 'Anup Patel' <[email protected]>
??: 'hchauhan at ventanamicro.com' <[email protected]>; 'opensbi at lists.infradead.org' <[email protected]>; 'chenchaokai at eswincomputing.com' <[email protected]> ??: ??: [PATCH 1/1] Add RISC-V TEE support

Hi, Anup
You mean this code?

+#define SBI_EXT_TEE_START                      0x0A000000
+#define SBI_EXT_TEE_END                        0x0AFFFFFF
+#define SBI_EXT_TEE                            0xFFFFEEEE

Current code is not in use SBI_EXT_TEE_START and SBI_EXT_TEE_END, I use SBI_EXT_TEE, I wrote these two macros in reference to SBI_EXT_FIRMWARE_START, I'd like your advice on this part.
Currently, there are 20 scenarios, 12 of which are reserved for later use. I am not familiar with the rules for the use of SBI extension ID and funciton ID. Because of the number of arguments, instead of using linux sbi_ecall, I encapsulated the ecall instruction by ourselves.
I are not sure if this approach meets the requirements of opensbi and linux, So I made a redundant macro definition here.
Could you give me some advice? Thank you.

Regards,
liushiwei

-----????-----
???: Anup Patel [mailto:apatel at ventanamicro.com]
????: 2023?1?11? 20:34
???: liushiwei <[email protected]>
??: hchauhan at ventanamicro.com; opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone
hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want?
The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

We can't blindly use SBI extension ID and function ID space for TEE.

Please share a draft proposal of how OP-TEE calls will be implemented as SBI calls.

I see that you have reserved an entire range of SBI extension IDs for OP-TEE. This is a waste of the SBI extension ID space.

Regards,
Anup

Quoted 49 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#6 lore raw
Quoted 1 line · liushiwei

Hi, Anup

It'd be nice if you'd respond inline so that following the conversation was easier. And responding from a mobile device would be too!

arch/riscv/include/asm/sbi.h enum sbi_ext_id { · line 32

Quoted 11 lines

I've combed through the linux code. What do you think of the following change?

arch/riscv/include/asm/sbi.h +1 -0
index d1c37479d..9696c8c77 100644
@@ -29,6 +29,7 @@ enum sbi_ext_id {
       SBI_EXT_RFENCE = 0x52464E43,
       SBI_EXT_HSM = 0x48534D,
       SBI_EXT_SRST = 0x53525354,
+       SBI_EXT_TEE = 0x544545,

This range is reserved for official extensions.
Can you please respond to Anup's request, preserved below, for a draft proposal?

Quoted 2 lines
arch/riscv/include/asm/sbi.h +0 -0
 	I find that these values are just transformations of these letters?So I just use the ext id, not the func id?
 	While the sbi_ecall_tee_handler function uses other registers, such as t0.
Quoted 2 lines · Anup
Quoted 2 lines

We can't blindly use SBI extension ID and function ID space for TEE. Please share a draft proposal of how OP-TEE calls will be implemented as SBI calls.

Thanks,
Conor.

#7 lore raw
In reply to liushiwei
Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 42 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 3 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#8 lore raw

Hi, Himanshu , These are my description:

In my design, The entire linux space is called REE (Rich Execution Environment), and TEE OS includes its user state called TEE (Trusted execution environment). adding TEE functionality to opensbi requires two configuration items. For example:
add CONFIG_SBI_ECALL_TEE=y in platform/generic/configs/defconfig file. add CONFIG_TEE_LOAD_ADDR=0x27C000000 platform/generic/objects.mk. The value of CONFIG_TEE_LOAD_ADDR depends on the actual memory layout, It's a physical address.

When TEE is configured, opensbi adds the following functionality:
1. Boot TEE OS.
If TEE is enabled, tee_os_init() is called before entering sbi_hart_switch_mode(). tee_os_init saves the current context,
sets a new trap stack address, and runs to CONFIG_TEE_LOAD_ADDR as configured to complete TEE OS initialization. TEE OS returns via ecall, Go to opensbi sbi_ecall_tee_handler and use the characteristic value RETURN_ENTRY_DONE
to indicate the return after the TEE OS completes booting. Check whether the TEE OS boot successfully according to the parameter. If fails,
the system enters wfi and terminates the startup process of opensbi. If successful, it returns REE to switch into TEE's vector. then trap stack memory is restored, the context is restored, the tee_os_init call is returned,
and the rest of the process is performed.
2. REE switches to TEE.
When the TEE OS boot successfully, it returns an entry vector for REE into the TEE. It represents various entry points into TEE OS and is stored in opensbi's global variables. It is the optee_vectors_t structure, which contains nine entry cases:
1. yield_smc_entry;
2. fast_smc_entry;
3. cpu_on_entry;
4. cpu_off_entry;
5. cpu_resume_entry;
6. cpu_suspend_entry;
7. fiq_entry;
8. system_off_entry;
9. system_reset_entry;
yield_smc_entry means that this function entry TEE will start the thread function and enter the user state of TEE. It may also switch back to REE with RPC function, and then return to TEE after REE completes the corresponding function.
For the REE process that sent you this call, it may cause sleep.

fast_smc_entry indicates that this is a quick function that returns after the TEE OS does something, and that it does not cause the caller to sleep.

yield_smc_entry and fast_smc_entry return opensbi use eigenvalue RETURN_CALL_DONE. for yield_smc_entry, whether the call returns or the RPC returns is decided by linux.

cpu_on_entry/cpu_off_entry/cpu_resume_entry/cpu_suspend_entry and fiq_entry/system_off_entry/system_reset_entry they are not implemented currently.

3. TEE switches to REE.
opensbi needs to save the context when REE enters the TEE, and restore the context when it returns from the TEE.

4. TEE/REE Request a special function.
We have some specific functions, like get hartid from TEE, it need save and restore the TEE context.

Data structure.
opensbi adds the sbi_save_context declaration

   struct sbi_save_context {
      struct sbi_trap_regs regs;
      unsigned long sepc;
      unsigned long satp;
      unsigned long sstatus;
      unsigned long sie;
      unsigned long stvec;
      unsigned long sscratch;
      unsigned long scounteren;
      unsigned long scause;
      unsigned long stval;
      unsigned long sip;

};
sbi_save_context include sbi_trap_regs and S mode csr. Used to hold the context of TEE or REE

   typedef struct optee_vectors {
      optee_vector_isn_t yield_smc_entry;
      optee_vector_isn_t fast_smc_entry;
      optee_vector_isn_t cpu_on_entry;
      optee_vector_isn_t cpu_off_entry;
      optee_vector_isn_t cpu_resume_entry;
      optee_vector_isn_t cpu_suspend_entry;
      optee_vector_isn_t fiq_entry;
      optee_vector_isn_t system_off_entry;
      optee_vector_isn_t system_reset_entry;
   } optee_vectors_t;
   optee_vectors represent the various entry points into TEE OS.

Variables defined by opensbi
optee_vectors_t *optee_vector_table;
struct sbi_save_context nsec_cpu_context[OPTEED_CORE_COUNT]; struct sbi_save_context sec_cpu_context[OPTEED_CORE_COUNT]; struct sbi_trap_regs cpu_start_context[OPTEED_CORE_COUNT]; typedef ulong tee_tmp_trap_stack[1024];
static tee_tmp_trap_stack tmp_stack[OPTEED_CORE_COUNT];

optee_vector_table value is assigned after the TEE OS boot succeeds. nsec_cpu_context and sec_cpu_context is to save the context of TEE and REE, cpu_start_context save the context of opensbi before tee_os_init enter TEE OS, tee_tmp_stack is the trap stack when TEE OS return tee_os_init.

Under the current design, REE does not enable interrupts when entering TEE, and the entire TEE, including opensbi, is the process context of linux. TEE processing must be brief and quick.

Regards,
liushiwei
-----????-----
???: Himanshu Chauhan [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 23:39
???: liushiwei <[email protected]>
??: opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone
hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want?
The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 43 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#9 lore raw
Quoted 103 lines · liushiwei

Hi, Himanshu , These are my description:

In my design, The entire linux space is called REE (Rich Execution Environment), and TEE OS includes its user state called TEE (Trusted execution environment). adding TEE functionality to opensbi requires two configuration items. For example:
add CONFIG_SBI_ECALL_TEE=y in platform/generic/configs/defconfig file. add CONFIG_TEE_LOAD_ADDR=0x27C000000 platform/generic/objects.mk. The value of CONFIG_TEE_LOAD_ADDR depends on the actual memory layout, It's a physical address.

When TEE is configured, opensbi adds the following functionality:
1. Boot TEE OS.
If TEE is enabled, tee_os_init() is called before entering sbi_hart_switch_mode(). tee_os_init saves the current context,
sets a new trap stack address, and runs to CONFIG_TEE_LOAD_ADDR as configured to complete TEE OS initialization. TEE OS returns via ecall, Go to opensbi sbi_ecall_tee_handler and use the characteristic value RETURN_ENTRY_DONE
to indicate the return after the TEE OS completes booting. Check whether the TEE OS boot successfully according to the parameter. If fails,
the system enters wfi and terminates the startup process of opensbi. If successful, it returns REE to switch into TEE's vector. then trap stack memory is restored, the context is restored, the tee_os_init call is returned,
and the rest of the process is performed.
2. REE switches to TEE.
When the TEE OS boot successfully, it returns an entry vector for REE into the TEE. It represents various entry points into TEE OS and is stored in opensbi's global variables. It is the optee_vectors_t structure, which contains nine entry cases:
1. yield_smc_entry;
2. fast_smc_entry;
3. cpu_on_entry;
4. cpu_off_entry;
5. cpu_resume_entry;
6. cpu_suspend_entry;
7. fiq_entry;
8. system_off_entry;
9. system_reset_entry;
yield_smc_entry means that this function entry TEE will start the thread function and enter the user state of TEE. It may also switch back to REE with RPC function, and then return to TEE after REE completes the corresponding function.
For the REE process that sent you this call, it may cause sleep.

fast_smc_entry indicates that this is a quick function that returns after the TEE OS does something, and that it does not cause the caller to sleep.

yield_smc_entry and fast_smc_entry return opensbi use eigenvalue RETURN_CALL_DONE. for yield_smc_entry, whether the call returns or the RPC returns is decided by linux.

cpu_on_entry/cpu_off_entry/cpu_resume_entry/cpu_suspend_entry and fiq_entry/system_off_entry/system_reset_entry they are not implemented currently.

3. TEE switches to REE.
opensbi needs to save the context when REE enters the TEE, and restore the context when it returns from the TEE.

4. TEE/REE Request a special function.
We have some specific functions, like get hartid from TEE, it need save and restore the TEE context.

Data structure.
opensbi adds the sbi_save_context declaration

   struct sbi_save_context {
      struct sbi_trap_regs regs;
      unsigned long sepc;
      unsigned long satp;
      unsigned long sstatus;
      unsigned long sie;
      unsigned long stvec;
      unsigned long sscratch;
      unsigned long scounteren;
      unsigned long scause;
      unsigned long stval;
      unsigned long sip;
   };
   sbi_save_context include sbi_trap_regs and S mode csr. Used to hold the context of TEE or REE

   typedef struct optee_vectors {
      optee_vector_isn_t yield_smc_entry;
      optee_vector_isn_t fast_smc_entry;
      optee_vector_isn_t cpu_on_entry;
      optee_vector_isn_t cpu_off_entry;
      optee_vector_isn_t cpu_resume_entry;
      optee_vector_isn_t cpu_suspend_entry;
      optee_vector_isn_t fiq_entry;
      optee_vector_isn_t system_off_entry;
      optee_vector_isn_t system_reset_entry;
   } optee_vectors_t;
   optee_vectors represent the various entry points into TEE OS.

Variables defined by opensbi

   optee_vectors_t *optee_vector_table;
   struct sbi_save_context nsec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_save_context sec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_trap_regs cpu_start_context[OPTEED_CORE_COUNT];
   typedef ulong tee_tmp_trap_stack[1024];
   static tee_tmp_trap_stack tmp_stack[OPTEED_CORE_COUNT];

   optee_vector_table value is assigned after the TEE OS boot succeeds.
   nsec_cpu_context and sec_cpu_context is to save the context of TEE and REE,
   cpu_start_context save the context of opensbi before tee_os_init enter TEE OS,
   tee_tmp_stack is the trap stack when TEE OS return tee_os_init.
Quoted 3 lines

Under the current design, REE does not enable interrupts when entering TEE, and the entire TEE, including opensbi, is the process context of linux. TEE processing must be brief and quick.

Looks like you do have some write-up for your proof-of-concept implementation which is good but we can look at your patches only after we have discussed your SBI OPTEE proposal.

I suggest you should create a textual/adoc description as if you are writing a chapter for https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc

After you have this textual/adoc writeup for SBI OPTEE, we can discuss this in the Platform runtime services (PRS) or Trusted computing SIG meeting.

Your patches can be reviewed only after there is enough agreement on your SBI OPTEE proposal.

Regards,
Anup

Quoted 71 lines

Regards,
liushiwei
-----????-----
???: Himanshu Chauhan [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 23:39
???: liushiwei <[email protected]>
??: opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone
hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want?
The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 35 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Quoted 8 lines

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#10 lore raw

Hi?Anup
I edit it in https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc. I tried to push a document edit commit [https://github.com/riscv-non-isa/riscv-sbi-doc/pull/106]. Please check. Am I doing what you expect me to do

Thanks
Liushiwei
-----????-----
???: Anup Patel [mailto:anup at brainfault.org]
????: 2023?1?13? 20:00
???: liushiwei <[email protected]>
??: Himanshu Chauhan <[email protected]>; opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 104 lines · liushiwei

Hi, Himanshu , These are my description:

In my design, The entire linux space is called REE (Rich Execution Environment), and TEE OS includes its user state called TEE (Trusted execution environment). adding TEE functionality to opensbi requires two configuration items.
For example:
add CONFIG_SBI_ECALL_TEE=y in platform/generic/configs/defconfig file.
add CONFIG_TEE_LOAD_ADDR=0x27C000000 platform/generic/objects.mk.
The value of CONFIG_TEE_LOAD_ADDR depends on the actual memory layout, It's a physical address.

When TEE is configured, opensbi adds the following functionality:
1. Boot TEE OS.
If TEE is enabled, tee_os_init() is called before entering sbi_hart_switch_mode(). tee_os_init saves the current context,
sets a new trap stack address, and runs to CONFIG_TEE_LOAD_ADDR as configured to complete TEE OS initialization. TEE OS returns via ecall, Go to opensbi sbi_ecall_tee_handler and use the characteristic value RETURN_ENTRY_DONE
to indicate the return after the TEE OS completes booting. Check whether the TEE OS boot successfully according to the parameter. If fails,
the system enters wfi and terminates the startup process of opensbi. If successful, it returns REE to switch into TEE's vector. then trap stack memory is restored, the context is restored, the tee_os_init call is returned,
and the rest of the process is performed.
2. REE switches to TEE.
When the TEE OS boot successfully, it returns an entry vector for REE into the TEE. It represents various entry points into TEE OS and is stored in opensbi's global variables. It is the optee_vectors_t structure, which contains nine entry cases:
1. yield_smc_entry;
2. fast_smc_entry;
3. cpu_on_entry;
4. cpu_off_entry;
5. cpu_resume_entry;
6. cpu_suspend_entry;
7. fiq_entry;
8. system_off_entry;
9. system_reset_entry;
yield_smc_entry means that this function entry TEE will start the thread function and enter the user state of TEE. It may also switch back to REE with RPC function, and then return to TEE after REE completes the corresponding function.
For the REE process that sent you this call, it may cause sleep.

fast_smc_entry indicates that this is a quick function that returns after the TEE OS does something, and that it does not cause the caller to sleep.

yield_smc_entry and fast_smc_entry return opensbi use eigenvalue RETURN_CALL_DONE. for yield_smc_entry, whether the call returns or the RPC returns is decided by linux.

cpu_on_entry/cpu_off_entry/cpu_resume_entry/cpu_suspend_entry and fiq_entry/system_off_entry/system_reset_entry they are not implemented currently.

3. TEE switches to REE.
opensbi needs to save the context when REE enters the TEE, and restore the context when it returns from the TEE.

4. TEE/REE Request a special function.
We have some specific functions, like get hartid from TEE, it need save and restore the TEE context.

Data structure.
opensbi adds the sbi_save_context declaration

   struct sbi_save_context {
      struct sbi_trap_regs regs;
      unsigned long sepc;
      unsigned long satp;
      unsigned long sstatus;
      unsigned long sie;
      unsigned long stvec;
      unsigned long sscratch;
      unsigned long scounteren;
      unsigned long scause;
      unsigned long stval;
      unsigned long sip;

};
sbi_save_context include sbi_trap_regs and S mode csr. Used to hold
the context of TEE or REE

   typedef struct optee_vectors {
      optee_vector_isn_t yield_smc_entry;
      optee_vector_isn_t fast_smc_entry;
      optee_vector_isn_t cpu_on_entry;
      optee_vector_isn_t cpu_off_entry;
      optee_vector_isn_t cpu_resume_entry;
      optee_vector_isn_t cpu_suspend_entry;
      optee_vector_isn_t fiq_entry;
      optee_vector_isn_t system_off_entry;
      optee_vector_isn_t system_reset_entry;
   } optee_vectors_t;
   optee_vectors represent the various entry points into TEE OS.

Variables defined by opensbi

   optee_vectors_t *optee_vector_table;
   struct sbi_save_context nsec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_save_context sec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_trap_regs cpu_start_context[OPTEED_CORE_COUNT];
   typedef ulong tee_tmp_trap_stack[1024];
   static tee_tmp_trap_stack tmp_stack[OPTEED_CORE_COUNT];

   optee_vector_table value is assigned after the TEE OS boot succeeds.
   nsec_cpu_context and sec_cpu_context is to save the context of TEE and REE,
   cpu_start_context save the context of opensbi before tee_os_init enter TEE OS,
   tee_tmp_stack is the trap stack when TEE OS return tee_os_init.
Quoted 3 lines

Under the current design, REE does not enable interrupts when entering TEE, and the entire TEE, including opensbi, is the process context of linux. TEE processing must be brief and quick.

Looks like you do have some write-up for your proof-of-concept implementation which is good but we can look at your patches only after we have discussed your SBI OPTEE proposal.

I suggest you should create a textual/adoc description as if you are writing a chapter for https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc

After you have this textual/adoc writeup for SBI OPTEE, we can discuss this in the Platform runtime services (PRS) or Trusted computing SIG meeting.

Your patches can be reviewed only after there is enough agreement on your SBI OPTEE proposal.

Regards,
Anup

Quoted 71 lines

Regards,
liushiwei
-----????-----
???: Himanshu Chauhan [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 23:39
???: liushiwei <[email protected]>
??: opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's
trustzone technology. optee os is a software solution using arm
trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed
these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 35 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE
OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for
context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Quoted 8 lines

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#11 lore raw

Hi Liushiwei,

Quoted 3 lines · liushiwei

Hi?Anup
I edit it in https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc. I tried to push a document edit commit [https://github.com/riscv-non-isa/riscv-sbi-doc/pull/106]. Please check. Am I doing what you expect me to do

This is a good starting point but can you add more details about:
1) Details of the supervisor state to be saved-n-restored by SBI implementation when forwarding the TEE calls
2) Details about each TEE function (such as FID, etc). You can cite the actual OP-TEE spec

Regards,
Anup

Quoted 203 lines

Thanks
Liushiwei
-----????-----
???: Anup Patel [mailto:anup at brainfault.org]
????: 2023?1?13? 20:00
???: liushiwei <[email protected]>
??: Himanshu Chauhan <[email protected]>; opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 104 lines · liushiwei

Hi, Himanshu , These are my description:

In my design, The entire linux space is called REE (Rich Execution Environment), and TEE OS includes its user state called TEE (Trusted execution environment). adding TEE functionality to opensbi requires two configuration items.
For example:
add CONFIG_SBI_ECALL_TEE=y in platform/generic/configs/defconfig file.
add CONFIG_TEE_LOAD_ADDR=0x27C000000 platform/generic/objects.mk.
The value of CONFIG_TEE_LOAD_ADDR depends on the actual memory layout, It's a physical address.

When TEE is configured, opensbi adds the following functionality:
1. Boot TEE OS.
If TEE is enabled, tee_os_init() is called before entering sbi_hart_switch_mode(). tee_os_init saves the current context,
sets a new trap stack address, and runs to CONFIG_TEE_LOAD_ADDR as configured to complete TEE OS initialization. TEE OS returns via ecall, Go to opensbi sbi_ecall_tee_handler and use the characteristic value RETURN_ENTRY_DONE
to indicate the return after the TEE OS completes booting. Check whether the TEE OS boot successfully according to the parameter. If fails,
the system enters wfi and terminates the startup process of opensbi. If successful, it returns REE to switch into TEE's vector. then trap stack memory is restored, the context is restored, the tee_os_init call is returned,
and the rest of the process is performed.
2. REE switches to TEE.
When the TEE OS boot successfully, it returns an entry vector for REE into the TEE. It represents various entry points into TEE OS and is stored in opensbi's global variables. It is the optee_vectors_t structure, which contains nine entry cases:
1. yield_smc_entry;
2. fast_smc_entry;
3. cpu_on_entry;
4. cpu_off_entry;
5. cpu_resume_entry;
6. cpu_suspend_entry;
7. fiq_entry;
8. system_off_entry;
9. system_reset_entry;
yield_smc_entry means that this function entry TEE will start the thread function and enter the user state of TEE. It may also switch back to REE with RPC function, and then return to TEE after REE completes the corresponding function.
For the REE process that sent you this call, it may cause sleep.

fast_smc_entry indicates that this is a quick function that returns after the TEE OS does something, and that it does not cause the caller to sleep.

yield_smc_entry and fast_smc_entry return opensbi use eigenvalue RETURN_CALL_DONE. for yield_smc_entry, whether the call returns or the RPC returns is decided by linux.

cpu_on_entry/cpu_off_entry/cpu_resume_entry/cpu_suspend_entry and fiq_entry/system_off_entry/system_reset_entry they are not implemented currently.

3. TEE switches to REE.
opensbi needs to save the context when REE enters the TEE, and restore the context when it returns from the TEE.

4. TEE/REE Request a special function.
We have some specific functions, like get hartid from TEE, it need save and restore the TEE context.

Data structure.
opensbi adds the sbi_save_context declaration

   struct sbi_save_context {
      struct sbi_trap_regs regs;
      unsigned long sepc;
      unsigned long satp;
      unsigned long sstatus;
      unsigned long sie;
      unsigned long stvec;
      unsigned long sscratch;
      unsigned long scounteren;
      unsigned long scause;
      unsigned long stval;
      unsigned long sip;

};
sbi_save_context include sbi_trap_regs and S mode csr. Used to hold
the context of TEE or REE

   typedef struct optee_vectors {
      optee_vector_isn_t yield_smc_entry;
      optee_vector_isn_t fast_smc_entry;
      optee_vector_isn_t cpu_on_entry;
      optee_vector_isn_t cpu_off_entry;
      optee_vector_isn_t cpu_resume_entry;
      optee_vector_isn_t cpu_suspend_entry;
      optee_vector_isn_t fiq_entry;
      optee_vector_isn_t system_off_entry;
      optee_vector_isn_t system_reset_entry;
   } optee_vectors_t;
   optee_vectors represent the various entry points into TEE OS.

Variables defined by opensbi

   optee_vectors_t *optee_vector_table;
   struct sbi_save_context nsec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_save_context sec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_trap_regs cpu_start_context[OPTEED_CORE_COUNT];
   typedef ulong tee_tmp_trap_stack[1024];
   static tee_tmp_trap_stack tmp_stack[OPTEED_CORE_COUNT];

   optee_vector_table value is assigned after the TEE OS boot succeeds.
   nsec_cpu_context and sec_cpu_context is to save the context of TEE and REE,
   cpu_start_context save the context of opensbi before tee_os_init enter TEE OS,
   tee_tmp_stack is the trap stack when TEE OS return tee_os_init.
Quoted 3 lines

Under the current design, REE does not enable interrupts when entering TEE, and the entire TEE, including opensbi, is the process context of linux. TEE processing must be brief and quick.

Looks like you do have some write-up for your proof-of-concept implementation which is good but we can look at your patches only after we have discussed your SBI OPTEE proposal.

I suggest you should create a textual/adoc description as if you are writing a chapter for https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc

After you have this textual/adoc writeup for SBI OPTEE, we can discuss this in the Platform runtime services (PRS) or Trusted computing SIG meeting.

Your patches can be reviewed only after there is enough agreement on your SBI OPTEE proposal.

Regards,
Anup

Quoted 71 lines

Regards,
liushiwei
-----????-----
???: Himanshu Chauhan [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 23:39
???: liushiwei <[email protected]>
??: opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's
trustzone technology. optee os is a software solution using arm
trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed
these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 35 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE
OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for
context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Quoted 8 lines

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#12 lore raw
In reply to liushiwei
Quoted 103 lines · liushiwei

Hi, Himanshu , These are my description:

In my design, The entire linux space is called REE (Rich Execution Environment), and TEE OS includes its user state called TEE (Trusted execution environment). adding TEE functionality to opensbi requires two configuration items. For example:
add CONFIG_SBI_ECALL_TEE=y in platform/generic/configs/defconfig file. add CONFIG_TEE_LOAD_ADDR=0x27C000000 platform/generic/objects.mk. The value of CONFIG_TEE_LOAD_ADDR depends on the actual memory layout, It's a physical address.

When TEE is configured, opensbi adds the following functionality:
1. Boot TEE OS.
If TEE is enabled, tee_os_init() is called before entering sbi_hart_switch_mode(). tee_os_init saves the current context,
sets a new trap stack address, and runs to CONFIG_TEE_LOAD_ADDR as configured to complete TEE OS initialization. TEE OS returns via ecall, Go to opensbi sbi_ecall_tee_handler and use the characteristic value RETURN_ENTRY_DONE
to indicate the return after the TEE OS completes booting. Check whether the TEE OS boot successfully according to the parameter. If fails,
the system enters wfi and terminates the startup process of opensbi. If successful, it returns REE to switch into TEE's vector. then trap stack memory is restored, the context is restored, the tee_os_init call is returned,
and the rest of the process is performed.
2. REE switches to TEE.
When the TEE OS boot successfully, it returns an entry vector for REE into the TEE. It represents various entry points into TEE OS and is stored in opensbi's global variables. It is the optee_vectors_t structure, which contains nine entry cases:
1. yield_smc_entry;
2. fast_smc_entry;
3. cpu_on_entry;
4. cpu_off_entry;
5. cpu_resume_entry;
6. cpu_suspend_entry;
7. fiq_entry;
8. system_off_entry;
9. system_reset_entry;
yield_smc_entry means that this function entry TEE will start the thread function and enter the user state of TEE. It may also switch back to REE with RPC function, and then return to TEE after REE completes the corresponding function.
For the REE process that sent you this call, it may cause sleep.

fast_smc_entry indicates that this is a quick function that returns after the TEE OS does something, and that it does not cause the caller to sleep.

yield_smc_entry and fast_smc_entry return opensbi use eigenvalue RETURN_CALL_DONE. for yield_smc_entry, whether the call returns or the RPC returns is decided by linux.

cpu_on_entry/cpu_off_entry/cpu_resume_entry/cpu_suspend_entry and fiq_entry/system_off_entry/system_reset_entry they are not implemented currently.

3. TEE switches to REE.
opensbi needs to save the context when REE enters the TEE, and restore the context when it returns from the TEE.

4. TEE/REE Request a special function.
We have some specific functions, like get hartid from TEE, it need save and restore the TEE context.

Data structure.
opensbi adds the sbi_save_context declaration

   struct sbi_save_context {
      struct sbi_trap_regs regs;
      unsigned long sepc;
      unsigned long satp;
      unsigned long sstatus;
      unsigned long sie;
      unsigned long stvec;
      unsigned long sscratch;
      unsigned long scounteren;
      unsigned long scause;
      unsigned long stval;
      unsigned long sip;
   };
   sbi_save_context include sbi_trap_regs and S mode csr. Used to hold the context of TEE or REE

   typedef struct optee_vectors {
      optee_vector_isn_t yield_smc_entry;
      optee_vector_isn_t fast_smc_entry;
      optee_vector_isn_t cpu_on_entry;
      optee_vector_isn_t cpu_off_entry;
      optee_vector_isn_t cpu_resume_entry;
      optee_vector_isn_t cpu_suspend_entry;
      optee_vector_isn_t fiq_entry;
      optee_vector_isn_t system_off_entry;
      optee_vector_isn_t system_reset_entry;
   } optee_vectors_t;
   optee_vectors represent the various entry points into TEE OS.

Variables defined by opensbi

   optee_vectors_t *optee_vector_table;
   struct sbi_save_context nsec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_save_context sec_cpu_context[OPTEED_CORE_COUNT];
   struct sbi_trap_regs cpu_start_context[OPTEED_CORE_COUNT];
   typedef ulong tee_tmp_trap_stack[1024];
   static tee_tmp_trap_stack tmp_stack[OPTEED_CORE_COUNT];

   optee_vector_table value is assigned after the TEE OS boot succeeds.
   nsec_cpu_context and sec_cpu_context is to save the context of TEE and REE,
   cpu_start_context save the context of opensbi before tee_os_init enter TEE OS,
   tee_tmp_stack is the trap stack when TEE OS return tee_os_init.
Quoted 4 lines

Under the current design, REE does not enable interrupts when entering TEE, and the entire TEE, including opensbi, is the process context of linux. TEE processing must be brief and quick.

Great. Thanks. Your PR should include more details like this with proper FID allocation and description.
In order to make it a standard RISC-V SBI extension, you need to present this in tech-prs mailing list
(https://lists.riscv.org/g/tech-prs)
and get it approved within the group. Here is the policy document[1]

https://docs.google.com/document/d/1bQGHU-wD4uN4mU07oH9adpXbH51NqUbFxcL8qTTIqhA/edit

I think it would be good if you can present your work in trusted computing SIG (https://lists.riscv.org/g/sig-trusted-computing) as well.

Quoted 70 lines

Regards,
liushiwei
-----????-----
???: Himanshu Chauhan [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 23:39
???: liushiwei <[email protected]>
??: opensbi at lists.infradead.org; chenchaokai at eswincomputing.com ??: Re: ??: [PATCH 1/1] Add RISC-V TEE support

Quoted 8 lines · liushiwei

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone
hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want?
The current committed code is not hardware-dependent, but just
continues the idea of this workaround, and we may commit hardware-dependent code later.

No, I meant the software specification.

Quoted 35 lines

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 4 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Quoted 8 lines

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 2 lines
Regards,
Atish
author
#13 lore raw
In reply to [email protected]

" Our hardware design referred to arm's trustzone technology", The translation software description may not be quite accurate, a hardware technology like arm trustzone might be more better, We want to submit a piece of common hardware-independent code to opensbi, and then add hardware-specific code controlled by configuration items to the common part, Would you accept such a commit?

-----????-----
???: liushiwei [mailto:liushiwei at eswincomputing.com] ????: 2023?1?11? 20:28
???: 'hchauhan at ventanamicro.com' <[email protected]>; 'opensbi at lists.infradead.org' <[email protected]> ??: 'chenchaokai at eswincomputing.com' <[email protected]> ??: ??: [PATCH 1/1] Add RISC-V TEE support

Do you mean hardware? Our hardware design referred to arm's trustzone technology. optee os is a software solution using arm trustzone hardware, which mainly includes REE(linux), TEE(optee os), ATF(ARM Trusted firmware), and then our software also developed these three parts. opensbi is similar to ATF. whether if this is what you want? The current committed code is not hardware-dependent, but just continues the idea of this workaround, and we may commit hardware-dependent code later.

-----????-----
???: hchauhan at ventanamicro.com [mailto:hchauhan at ventanamicro.com] ????: 2023?1?11? 20:03
???: 'liushiwei' <[email protected]>; opensbi at lists.infradead. org
??: chenchaokai at eswincomputing.com
??: RE: [PATCH 1/1] Add RISC-V TEE support

-----Original Message-----

Quoted 5 lines

From: opensbi <[email protected]> On Behalf Of liushiwei
Sent: 11 January 2023 07:32
To: opensbi at lists.infradead.org
Cc: chenchaokai at eswincomputing.com; liushiwei

<[email protected]>

Quoted 3 lines

Subject: [PATCH 1/1] Add RISC-V TEE support

RISC-V Trusted Executable Environment security software includes linux,

opensbi, and OP-TEE OS. linux is the non-secure domain, and OP-TEE OS is the secure domain. At boot time, opensbi boots OP->TEE OS and then starts linux. At runtime, opensbi acts as a secure monitor, responsible for context saving and restoring when switching between linux and OP-TEE OS.

Quoted 2 lines

TEE function is off by default, when using configuration is added in the

config and objects file, such as platform/generic/configs/defconfig add CONFIG_SBI_ECALL_TEE = y, In the >platform/generic/objects.mk add CONFIG_TEE_LOAD_ADDR = 0x27c000000, CONFIG_TEE_LOAD_ADDR is the starting address of the OP-TEE OS.

Hi Liushiwei,

Was there any formal specification or draft for this? Could you please point me to the draft or specification?

Regards
Himanshu

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi