👀 本文以
ARM 32
架构下的u-boot源码来进行分析。u-boot版本:4.1.15
在u-boot源码中,随处可见gd这个宏,那么本文就来看看这个神秘的gd。本文包含两个主题:(1)如何定义gd。(2)gd_t结构体定义。
一、gd的定义
在u-boot源码中,对于ARM32架构,会使用r9
寄存器来表示全局的数据结构gd。以下代码片段出自
/arch/arm/include/asm/global_data.h
#ifdef __clang__
#define DECLARE_GLOBAL_DATA_PTR
#define gd get_gd()
static inline gd_t *get_gd(void)
{
gd_t *gd_ptr;
#ifdef CONFIG_ARM64
/*
* Make will already error that reserving x18 is not supported at the
* time of writing, clang: error: unknown argument: '-ffixed-x18'
*/
__asm__ volatile("mov %0, x18\n" : "=r" (gd_ptr));
#else
//arm 32bit下对应的gd
__asm__ volatile("mov %0, r9\n" : "=r" (gd_ptr));
#endif
return gd_ptr;
}
#else
#ifdef CONFIG_ARM64
#define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("x18")
#else
#define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("r9")
#endif
#endif
如上述代码所示,如果系统不是ARM64和clang时,代码最后则变成了:
#define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("r9")
分析至此,这里小生产生了一个疑问:如何定义一个寄存器变量呢? 根据GCC使用手册可知定义寄存器变量的方法。定义格式如下:
register int *foo asm ("reg");
其中register
关键字是必须的,asm ("reg")
为嵌入式汇编,因此,register volatile gd_t *gd asm (“r9”)则表示:用r9寄存器存储gd指针,r9寄存器和CPU体系结构相关。需要注意的是:不能使用static
、const
和volatile
等限定符,否则,可能与预期效果不同。特别需要注意的是volatile,就算加了也阻止不了编译器优化(因为此处在u-boot中的定义是不符合GCC编译器规范的,如果用grep DECLARE_GLOBAL_DATA_PTR -nr
命令在u-boot源码中查找,就会发现有的定义就没有使用volatile进行修饰)。
定义寄存器变量,单有上面的写法还不行。因为,虽然声明了使用reg来表示foo指针,但编译器还是会使用reg这个寄存器去做其他的事情(例如:参数传递等等)。所以如果想让编译器不把reg寄存器用作其他用途,则还需要使用-ffixed-reg
编译选项显式的告诉编译器reg寄存器是一个固定用途的寄存器,不能把它用作其他用途。
在u-boot源码下,对应的arch/arm/config.mk文件中有如下变量定义,可以找到‘-ffixed-reg’选项:
PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections \
-fno-common -ffixed-r9
因此编译器在编译u-boot时,则将r9寄存器视为了一个固定寄存器。
gd是一个全局的指针变量,那么该变量指向的是gd_t
结构体类型数据,然而gd变量在定义时是没有被初始化,即没有被赋予初值,那么gd变量是在哪个地方被赋予初值的呢?下文将分析这一点。
二、gd_t结构体定义
从上文可知,gd变量是一个指向gd_t结构体的指针,gd_t结构体定义如下,代码片段出自include/asm-generic/global_data.h
typedef struct global_data {
bd_t *bd;
unsigned long flags;
unsigned int baudrate;
unsigned long cpu_clk; /* CPU clock in Hz! */
unsigned long bus_clk;
/* We cannot bracket this with CONFIG_PCI due to mpc5xxx */
unsigned long pci_clk;
unsigned long mem_clk;
#if defined(CONFIG_LCD) || defined(CONFIG_VIDEO)
unsigned long fb_base; /* Base address of framebuffer mem */
#endif
#if defined(CONFIG_POST) || defined(CONFIG_LOGBUFFER)
unsigned long post_log_word; /* Record POST activities */
unsigned long post_log_res; /* success of POST test */
unsigned long post_init_f_time; /* When post_init_f started */
#endif
#ifdef CONFIG_BOARD_TYPES
unsigned long board_type;
#endif
unsigned long have_console; /* serial_init() was called */
#ifdef CONFIG_PRE_CONSOLE_BUFFER
unsigned long precon_buf_idx; /* Pre-Console buffer index */
#endif
unsigned long env_addr; /* Address of Environment struct */
unsigned long env_valid; /* Checksum of Environment valid? */
unsigned long ram_top; /* Top address of RAM used by U-Boot */
unsigned long relocaddr; /* Start address of U-Boot in RAM */
phys_size_t ram_size; /* RAM size */
#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
#define MEM_RESERVE_SECURE_SECURED 0x1
#define MEM_RESERVE_SECURE_MAINTAINED 0x2
#define MEM_RESERVE_SECURE_ADDR_MASK (~0x3)
/*
* Secure memory addr
* This variable needs maintenance if the RAM base is not zero,
* or if RAM splits into non-consecutive banks. It also has a
* flag indicating the secure memory is marked as secure by MMU.
* Flags used: 0x1 secured
* 0x2 maintained
*/
phys_addr_t secure_ram;
#endif
unsigned long mon_len; /* monitor len */
unsigned long irq_sp; /* irq stack pointer */
unsigned long start_addr_sp; /* start_addr_stackpointer */
unsigned long reloc_off;
struct global_data *new_gd; /* relocated global data */
#ifdef CONFIG_DM
struct udevice *dm_root; /* Root instance for Driver Model */
struct udevice *dm_root_f; /* Pre-relocation root instance */
struct list_head uclass_root; /* Head of core tree */
#endif
#ifdef CONFIG_TIMER
struct udevice *timer; /* Timer instance for Driver Model */
#endif
const void *fdt_blob; /* Our device tree, NULL if none */
void *new_fdt; /* Relocated FDT */
unsigned long fdt_size; /* Space reserved for relocated FDT */
struct jt_funcs *jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
#ifdef CONFIG_TRACE
void *trace_buff; /* The trace buffer */
#endif
#if defined(CONFIG_SYS_I2C)
int cur_i2c_bus; /* current used i2c bus */
#endif
#ifdef CONFIG_SYS_I2C_MXC
void *srdata[10];
#endif
unsigned long timebase_h;
unsigned long timebase_l;
#ifdef CONFIG_SYS_MALLOC_F_LEN
unsigned long malloc_base; /* base address of early malloc() */
unsigned long malloc_limit; /* limit address */
unsigned long malloc_ptr; /* current address */
#endif
#ifdef CONFIG_PCI
struct pci_controller *hose; /* PCI hose for early use */
phys_addr_t pci_ram_top; /* top of region accessible to PCI */
#endif
#ifdef CONFIG_PCI_BOOTDELAY
int pcidelay_done;
#endif
struct udevice *cur_serial_dev; //当前的串口设备
struct arch_global_data arch; /* architecture-specific data */
#ifdef CONFIG_CONSOLE_RECORD
struct membuff console_out; /* console output */
struct membuff console_in; /* console input */
#endif
#ifdef CONFIG_DM_VIDEO
ulong video_top; /* Top of video frame buffer area */
ulong video_bottom; /* Bottom of video frame buffer area */
#endif
} gd_t;
对于gd_t结构体类型:
(1)每一种架构体系都有其私有特定的数据字段。
(2)以上所定义的数据将被放置在系统启动后早期可用的内存中,例如MPC8xx/MPC82xx上的DPRAM或者被锁定的数据缓存中。用于在系统的内存控制器还没有设置好之前,在系统初始化期间能够存放最小的全局变量集。
三、初始化gd的值
如上图所示,该代码出自/arch/arm/lib/crt0.S文件中,第89行汇编代码表示:将r0中的值赋值给r9,因为r9存放的是gd变量,因此gd变量在此刻就被赋予初始值了。然后,第90行代码调用board_init_f_init_reserve()
函数,用于调整gd指向的空间和预留gd指向数据的内存空间。
board_init_f_init_reserve()
函数定义在common/init/board_init.c文件中,也独立于特定体系架构。如下代码片段所示:
void board_init_f_init_reserve(ulong base)
{
struct global_data *gd_ptr;
#ifndef _USE_MEMCPY
int *ptr;
#endif
/*
* clear GD entirely and set it up.
* Use gd_ptr, as gd may not be properly set yet.
*/
gd_ptr = (struct global_data *)base;
/* zero the area */
#ifdef _USE_MEMCPY
memset(gd_ptr, '\0', sizeof(*gd));
#else
for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
*ptr++ = 0;
#endif
/* set GD unless architecture did it already */
#if !defined(CONFIG_ARM)
arch_setup_gd(gd_ptr);
#endif
/* next alloc will be higher by one GD plus 16-byte alignment */
base += roundup(sizeof(struct global_data), 16);
/*
* record early malloc arena start.
* Use gd as it is now properly set for all architectures.
*/
#if defined(CONFIG_SYS_MALLOC_F)
/* go down one 'early malloc arena' */
gd->malloc_base = base;
/* next alloc will be higher by one 'early malloc arena' size */
base += CONFIG_SYS_MALLOC_F_LEN;
#endif
}
四、总结
在u-boot源码中,随处可见gd。gd本质上是一个宏定义,用于获取指向gd_t
结构的指针。
👉搜索关注【嵌入式小生】wx公众号获取更多精彩内容