EDA365电子论坛网

标题: ARM平台下Uboot启动Linux内核前的必备条件 [打印本页]

作者: piday123    时间: 2020-6-10 16:41
标题: ARM平台下Uboot启动Linux内核前的必备条件

! I6 a, y% i$ W: cLinux内核是由Uboot进行调用的,在执行内核前,需要满足什么条件呢?  ~+ f' ]5 [5 j) Q
( d& h; @& a) k1 f: G* R6 o
我们来看下内核文档Documentation/arm/booting。内核版本为3.7.6。7 s' ]6 s& ^+ S! W) H; Q
* {$ P& }6 `+ F$ u. B  Y" Y
。。。。。。& ^) e& e! ^% f5 E% n% {! S# B2 n
" B) z7 t; U6 y% ^
* ?  Y! d  i+ n( e: k$ \, }
0 d- N% c' ^; Q! `* l) E
5. Calling the kernel image
0 J$ ]  x( n# n---------------------------3 Q, Q7 g+ {4 [
3 e0 ?; Z) K, F8 x' z, s) Z
Existing boot loaders:        MANDATORY
, p' r9 g+ r% G: z  V4 XNew boot loaders:        MANDATORY
6 B0 }6 Y" L8 \) a5 [4 u$ E% d, U( B
There are two options for calling the kernel zImage.  If the zImage
+ z% p3 P6 X( R# Ois stored in flash, and is linked correctly to be run from flash,
& ~6 m! _' v; ^7 G. g4 ithen it is legal for the boot loader to call the zImage in flash
7 |0 F6 Q8 z7 B# Tdirectly.
0 y$ g2 A0 ]" e- Q0 p3 ^/ C+ Y% L$ ^* q) \: M2 u
The zImage may also be placed in system RAM (at any location) and
, l( e, g  w; Q- ncalled there.  Note that the kernel uses 16K of RAM below the image/ q8 u  ~! y: p2 F) W% u
to store page tables.  The recommended placement is 32KiB into RAM.# K( c" O; Q" K7 f, X
. @, m) p. Q; V1 z8 N+ b3 ^
In either case, the following conditions must be met:
: q- M$ f9 O5 `- o) D" ^8 ]! A" C, ^9 n4 r% G
- Quiesce all DMA capable devices so that memory does not get1 _4 `, a1 b' I+ z
  corrupted by bogus network packets or disk data. This will save% U1 G( l6 v; ~& j
  you many hours of debug.
# ?& I% [* v: W& y: P, b( D) l7 o( v- l6 V6 p7 h9 i
- CPU register settings
" Q0 k) d- ^0 F# q% H  r0 = 0,6 i) v  H' r; K5 a* G7 M  h8 G
  r1 = machine type number discovered in (3) above.! L# @* l+ S" ]$ V( R1 v' _
  r2 = physical address of tagged list in system RAM, or( n( L5 ]- Z1 o
       physical address of device tree block (dtb) in system RAM
2 R; R) Y( S" O9 `3 A) F; q
, m; U: a. n2 C/ ]- CPU mode8 t2 P; w0 c* R0 h
  All forms of interrupts must be disabled (IRQs and FIQs)
2 b( ^  h0 ^1 t' |* U7 l
+ b" ^: g: T1 C+ `( j  For CPUs which do not include the ARM virtualization extensions, the, r  L0 v. A" s# Z$ b% p1 f
  CPU must be in SVC mode.  (A special exception exists for Angel)
0 i0 b( J5 ]6 S( Z6 Q
4 i( ]6 p8 }! N& Q5 t2 O- o" V  CPUs which include support for the virtualization extensions can be
, K7 d  b: u' v& n6 W5 v2 M7 K  entered in HYP mode in order to enable the kernel to make full use of
& O2 E  V0 ]2 H+ i  these extensions.  This is the recommended boot method for such CPUs,
6 W" o7 ^; m( q( N& Z5 r  unless the virtualisations are already in use by a pre-installed
: p& R+ A, @1 }  hypervisor.5 e/ i4 C4 o0 \) e* b* i/ d. w

( Z' c4 R+ v8 ~( W  If the kernel is not entered in HYP mode for any reason, it must be/ \0 x* c+ J4 u* p4 d
  entered in SVC mode.8 l) @1 u! T+ X: b- G, b7 d  M& d
3 ]( V: E1 v* f5 W) w
- Caches, MMUs" U* g) a5 M. Z! z. j$ X6 c& r+ |
  The MMU must be off.
: I5 s/ z( ^8 S& ^  Instruction cache may be on or off.
9 Q: i: B9 _" p: D' V. u  Data cache must be off.
2 b! o! ]( @3 v, P  Y
( C6 I; U/ O" u  N; [3 x  If the kernel is entered in HYP mode, the above requirements apply to
4 Z! |' \6 t/ \% y( Q  the HYP mode configuration in addition to the ordinary PL1 (privileged
, J6 E; r( }$ x2 r2 P$ u/ f0 ~  kernel modes) configuration.  In addition, all traps into the7 \5 u+ J* k& _  x2 w
  hypervisor must be disabled, and PL1 access must be granted for all
; {& x( P/ A4 L( e3 N6 p  peripherals and CPU resources for which this is architecturally
" o% ]" i$ O6 i/ G% G- Z  possible.  Except for entering in HYP mode, the system configuration
8 K5 Q/ h; y: L  N) I  should be such that a kernel which does not include support for the
+ ?8 P# @$ ?/ T2 V  virtualization extensions can boot correctly without extra help.7 @* F3 v) ]" e8 W0 K8 |+ F3 T/ E

( a$ R; q: d# A  M- The boot loader is expected to call the kernel image by jumping; r! s, M0 ?, x( O. ^. T
  directly to the first instruction of the kernel image.
2 z: d* N! R9 e0 k  F% `( X6 Y. @) g
# j0 n+ `6 w& H& B, l; L  On CPUs supporting the ARM instruction set, the entry must be
0 [; g! D5 R& f9 e  made in ARM state, even for a Thumb-2 kernel.
& T3 K6 K6 o# s+ h" J7 k- r& M3 g1 M. n
  On CPUs supporting only the Thumb instruction set such as9 N  q; j$ }4 j+ T% ~0 j8 [  Y5 k
  Cortex-M class CPUs, the entry must be made in Thumb state.
* V6 y3 X) l# q+ g# j; y8 Y% t; D6 S# S1 J5 C" Z4 P! S1 i  H  h3 Y
9 n0 ^7 O& c( s$ W! I# }9 I9 [
这里,省略了该文档中不感兴趣的部分。
& k6 V, z; e8 _$ l  M2 N1 {0 P- ]8 V' ~; W5 t; G7 f# M
根据文档,5 C- t+ i+ R7 B( V. c
) ?) ^7 W$ ?) ^! g2 w
第一, 必须禁止所有使用DMA的设备。 感觉这个有点多余,Uboot使用的设备,包括网卡之类的,一般都不会使用DMA。3 {, |; W. V; M1 j+ t, h3 E

. w: M! \9 i7 }第二,必须设置r0,r1和r2寄存器为相应的值。这个是由下面的函数调用实现的。
, d- R& r/ _" M0 `, s# A$ u
$ s% k9 T) a* b" ?' S0 S该函数在Uboot/lib_arm/armlinux.c的do_bootm_linux函数中被调用。( m) |0 r4 L1 ?& X6 ^7 u
& w; ]% s, i& {4 V: O* y
    theKernel (0, bd->bi_arch_number, bd->bi_boot_params);  q6 ~) L) I5 q6 `( a" ^* r8 F
- ?9 v  l6 s, h4 v# o1 ?& _9 J3 Q& ?
根据ATPCS的规则,函数的实参将分别传入r0到r3寄存器。这里有正好传入三个参数对应着0,机器码和参数列表的地址。
& z9 F# Y3 ]) d- ^# p1 ]0 `  L4 I, ]" B9 `3 H
第三,必须禁止中断,并且将CPU置入SVC(管理)模式。这些工作已经由start.S完成了。3 c" d- X% W' n" _" }7 s% g

! d2 a) q. z) m& A. I! ~  J第四,必须禁止数据cache和MMU,可以使能或禁止指令cache。这个是由下面的函数调用实现的。" R9 Z* k* T( _" X* T; j( l, y
1 J6 t4 @! x# @: [3 d# u, S6 q
该函数位在Uboot/lib_arm/armlinux.c的do_bootm_linux函数中被调用。
3 V$ P, e. b" r1 z5 A+ p0 h6 _
- _$ W9 ^- o( R' J; Z0 \  p2 V+ z下列函数位于:Uboot/cpu/arm920t/cpu.c中。: @- F3 H8 s0 [. {$ g9 W
2 H5 f5 I, `0 x) {
int cleanup_before_linux (void)
3 _: ^3 O2 j7 r4 e" t{& Z; h3 s. K/ R* O5 Q7 ]1 Q
        /*
7 U9 {' F' k1 X3 ^5 C4 L9 n& M: j( N         * this function is called just before we call linux
. p) C# R0 V" L% U9 M         * it prepares the processor for linux( l8 O" v: U* @% }& ]
         *' S. c, Y. I- L& q
         * we turn off caches etc ...5 l! E3 ]" q9 M0 u
         */
, L' Z  Q! g( }9 A7 ]
7 C0 ?7 i! s+ ^5 u4 A: I2 S        unsigned long i;
1 T3 P1 |6 V. X4 ^/ }4 r0 n
# {3 d& V5 O4 _8 Q        disable_interrupts ();
( G/ R( N2 m7 c # P+ N- s+ ~9 H1 t+ d
        /* turn off I/D-cache */
; t& L$ q2 ?! T% }5 X' x        asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));4 l2 c1 u; E5 {& u9 Q1 e- X' H
        i &= ~(C1_DC | C1_IC);
* v& x- U1 D3 J* Y. }        asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));, I5 G/ U% v* m& [2 Z+ Y; _" L
" I8 ~; z7 e( `7 {+ Q6 u; E. a
        /* flush I/D-cache */
: Q4 B; ^: T" Y2 s        i = 0;
3 L- R4 t* l, I1 f+ F* V* N  V        asm ("mcr p15, 0, %0, c7, c7, 0": :"r" (i));
. r7 e+ U$ k* ]! @4 c6 V- h- N 1 |( M- n5 ^* F, {5 J1 W0 X
        return (0);
  {) v* M& Y& u  X5 ~* ~}. g$ x# A1 ^$ W

- p0 ?- a8 r( D. h可以看出该函数禁止了数据和指令cache,同时冲刷了cache。
7 W# `& |' ?1 |; |1 {/ X( O这里并没有禁止MMU,MMU禁止是在start.S中完成的。7 T: M0 f. p  e7 \

3 O, C4 t; C, C4 \7 C' A
# z, \; q, W, N2 P
5 b- [; o# |5 L* q: H
7 F* d- ?( ^' _/ W
* P8 s# P/ R" |/ L# i( s. W
* L) ?) i- T! _2 Q* Y( p& \. W# W, s

$ \2 x2 j6 I7 w" S) J, H3 m" p9 H: ]6 `

作者: xiaogegepcb    时间: 2020-6-10 19:05
ARM平台下Uboot启动Linux内核前的必备条件




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2