TrustZone初探 (三)

来源:互联网 发布:房屋装修效果图软件 编辑:程序博客网 时间:2024/05/20 10:57
ARM网站上有个trustzone helloworld的例子,据说可以在fast model上模拟运行。我没去试,而是直接拿过来移植到自己的平台上,在u-boot下实现了。
代码在这:.

在我的例子里,u-boot本身就是在安全世界下运行的,所以我只要实现一个命令来做安全初始化,再写一个小的normal程序,先load normal程序到DDR,在执行这个命令设置安全环境,然后直接跳转至normal运行,normal跳回安全环境后打印再smc跳回,如此反复。几点经验:
1.  准备jtag调试器,否则寸步难行。
2. 这个例子虽然代码不多,但是信息量巨大,已经涵盖了ARM Secure extension的基本知识点,想成功在其他环境跑起来并非易事。
  |-> /headers
  |     |-> v7.h                  C header file for misc ARMv7-A helper functions
  |-> /obj                        This is where generated objected files will be placed
  |-> /src
  |     |-> v7.s                  Implementation of misc ARMv7-A helper functions
  |     |-> main_normal.c         main() for the Normal world
  |     |-> main_secure.c         main() for the Secure world
  |     |-> monitor.c             Code for the Secure Monitor
  |     |-> retarget_normal.c     Wrapper for main(), for Normal world
  |     |-> retarget_secure.c     Wrapper for main(), for Secure world
  |     |-> startup_normal.s      Initialization code for Normal world
  |     |-> startup_secure.s      Initialization code for Secure world
  |-> build.bat                   Build script for DOS
  |-> build.sh                    Build script for BASH
  |-> ReadMe.txt                  This file
  |-> scatter_secure.txt          scatter file for the Secure world image
  |-> scatter_normal.txt          scatter file for the Normal world image
  |-> normal.axf                  Debug symbols for Normal world
  |-> secure.axf                  Debug symbols for Secure world, code for both worlds
 
 
Description
============

Execution flow
---------------

  secureStart     startup_secure.s: Initialization of Secure world
       |
    __main        ARM library initialization
       |
  $Sub$$main      retarget_secure.s: Enable caches
       |
  monitorInit     monitor.s: Initialize Monitor and call NS world
       |
  << S -> NS >>
       |
  normalStart     startup_normal.s: Initialization of Normal world
       |
    __main        ARM library initialization
       |
  $Sub$$main      retarget_normal.s: Enable caches
       |
     main         main_normal.c: Print message and execute SMC
       |
  << NS -> S >>
       |
  SMC_Handler     monitor.s: Perform context switch from NS to S
       |
   $Sub$$main     retarget_secure.s: call Secure world's main()
       |
     main         main_secure.c: Print message and execute SMC
       |
  SMC_Handler     monitor.s: Perform context switch from NS to S
       |
  << S -> NS >>
       |
     main         main_normal.c: Print message and execute SMC
       |
  << NS -> S >>
       |
  SMC_Handler     monitor.s: Perform context switch from NS to S
       |
     main         main_secure.c: Print message and execute SMC

3.  这个例子埋了两个坑
a. movs pc,lr
这个指令是arm的中断返回指令,在改pc之前先把当前模式的spsr替换cpsr,是模式转换的重要指令。
但是一旦cpsr的值有点问题,那么等待你的就是奇怪的data abort。原来的例子代码这里可能会有一个坑,取决于你之前secure world的设置。
b. sp指针的上下文切换
两个世界可是共享sp_svc和sp_usr寄存器的,所以要保护好,那么问题来了,进到monitor模式以后sp已经是sp_mon,怎么拿到原来的sp? 原来的代码没实现,又是另外一个坑。

0 0
原创粉丝点击