[Uboot] (Foreign article) Global_data Introduction

Source: Internet
Author: User
Tags volatile

The following examples take the example of Project X projects tiny210 (s5pv210 platform, ARMV7 architecture)

[Uboot] uboot process series :
[Project X] tiny210 (s5pv210) power-on START process (BL0-BL2)
[Uboot] (chapter I) uboot process--Overview
[Uboot] (chapter II) uboot process--UBOOT-SPL compilation process

=================================================================================== One, Global_data function 1, the significance of Global_data existence

in some cases, the uboot is run on some read-only memory, such as ROM, nor flash, and so on.
before Uboot is redirected to RAM (readable writable), we are unable to write data, and we cannot pass the data through global variables.
And Global_data is to solve this problem.
Here by the way, the follow-up of the Uboot relocation operation, that is, the Uboot redirection operation, the main purpose is to solve the problem, the follow-up will be specifically explained. 2, Global_data simple introduction

Global_data also called GD.
simply put, Uboot puts Global_data in the Ram area and uses Global_data to store global data. This solves the problem of the inability to use global variables in the above scenarios. ii. global_data data structure 1, data structure description

The

Global_data data structure body is defined as a struct global_data and is gd_t by a typedef.
means that the declaration can be made directly from the struct global_data or gd_t. The
struct global_data is defined as follows (filtering out some of the parts that are included in the macro definition):
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 and CONFIG_PCI due to mpc5xxx */unsigned long pci_clk;
    unsigned long mem_clk; unsigned long have_console; /* Serial_init () was called */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 */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 */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.
    */unsigned long timebase_h;
    unsigned long timebase_l; struct Udevice *cur_serial_dev;   /* Current serial device */struct arch_global_data arch; /* architecture-specific data */} gd_t;
2. Member DescriptionKey notes
bd_t *bd:board Info Data structure definition, located in the file include/asm-arm/u-boot.h definition, is mainly to save the relevant parameters of the Development Board. unsigned long env_addr: The address of the environment variable. unsigned long ram_top:ram the top address of the space unsigned long relocaddr:uboot redirect address phys_size_t ram_size: size of physical RAM unsigned long irq_ SP: Broken stack address unsigned long start_addr_sp: Stack address unsigned long reloc_off:uboot relocation offset struct Global_data *new_ GD: redirected struct global_data struct const void *fdt_blob: DTB address of our device after void *new_fdt:relocation DTB address unsigned long fdt_ Size:dtb length struct Udevice *cur_serial_dev: The serial device currently in use.

Other members are explained at a later time. third, Global_data storage location and how to get its address 1. Global_data Regional Setup Code

(1) First refer to the code for assigning global_data.
Common/init/board_init.c

This function is used to allocate space to the Global_data, before relocation the call
//pass in the parameter is the top address, but not necessarily the memory top address, you can plan yourself, the following _main function will explain the
ulong Board_ Init_f_alloc_reserve (ULONG top)
{/
    * Reserve Early malloc Arena *
/#if defined (config_sys_malloc_f)
    Top-= Config_sys_malloc_f_len;
Assign a Config_sys_malloc_f_len size space from the top down to early MALLOC use
//about Config_sys_malloc_f_len can refer to the Readme
// This memory is used to provide a pool of memory to the malloc function before relocation.
#endif/

    * Last:reserve GD (rounded up to a multiple of bytes) */
    top = RoundDown (top-sizeof (struct GLO Bal_data);
Continue to allocate the size of sizeof (struct global_data) memory to global_data use, down 16byte to align//this time the
address is global_data.

    return top;
Return the address of top, which is Global_data
}

(2) then look at the code that initializes the Global_data area.
Common/init/board_init.c
Remove parts of irrelevant code

This function is used to initialize the Global_data area, that is, to empty the Global_data zone//The incoming parameter is Global_data's base address void Board_init_f_init_reserve (ULONG base)

    {struct Global_data *gd_ptr;
     /* * Clear GD entirely and set it up.
     * Use Gd_ptr, as GD could not properly set yet.
    */gd_ptr = (struct global_data *) base;
/* Zero the area */memset (gd_ptr, ' + ', sizeof (*GD)); The GLOBAL_DATA data structure is cleared by the Memset function first 0/* Next alloc will is higher by one GD plus 16-byte alignment */base + round
Up (sizeof (struct global_data), 16); Because the Global_data area is 16Byte aligned, the following address is the address of early malloc's memory pool, referring to the above board_init_f_alloc_reserve//So here is the early
     Address of the memory pool of malloc */* Record early malloc arena start.
     * Use GD as it's now properly set for all architectures.
*/#if defined (config_sys_malloc_f)/* Go down one ' early MALLOC arena ' */gd->malloc_base = base; Writes the address of the memory pool to the gd->malloc_base/* Next alloc will is higher by one ' early malloc arena ' size */base + = config_s Ys_mAlloc_f_len;
Plus Config_sys_malloc_f_len, gets the end address of the memory pool of early malloc, which does nothing to facilitate subsequent modifications to add an area behind the memory pool of the early malloc. #endif}

(3) How the arm platform allocates the Global_data zone and saves its address. The
code is as follows to remove the unrelated code part
Arch/arm/lib/crt0 that is contained by the macro definition. S

ENTRY (_main)/* * Set up initial C runtime environment and call Board_init_f (0). */Ldr SP, = (config_sys_init_sp_addr) @@ 预设 stack pointer is config_sys_init_sp_addr @@ 预设 tiny210 is initially set as follows (Include/configs/tiny210.h ): @@ zzfcthotfixz config_sys_sdram_base 0x20000000 @@ zzfcthotfixz memory_base_address config_sys_sdram_base @@ ZZFCTHOTFIXZ Phys_sdram_1 memory_base_address @@ zzfcthotfixz config_sys_load_addr (phys_sdram_1 + 0x1000000)/* Default LOAD Addre SS */@@ ZZFCTHOTFIXZ config_sys_init_sp_addr config_sys_load_addr @@ ZZFCTHOTFIXZ finally get Config_sys_init_sp_addr is 0x3000_0000, that is Uboot Relocation Start Address @@ 补充 Add, DDR space is 0x2000_0000-0x4000_0000 @@ 补充 ...

    This is just the default stack address, not the final stack address ...  Bic sp, SP, #7/* 8-byte alignment for ABI compliance */@@ 8byte aligned mov r0, SP BL Board_init_f_alloc_reserve @@ Put the value of the SP into R0, that is, as the parameters of Board_init_f_alloc_reserve @@ 返回, R0 is stored in Global_data address @@ 返回, but also the stack address, because the stack is growing downward, So do not have to worry about and Global_data conflict problem @@ 综 at this time R0 storage, is both the address of Global_data, is also the address of the stack mov sp, r0 @@ 综 the stack address r0 stored in the SP/* Set up GDHere, outside any C code */mov R9, r0 @@ 把 global_data address in R9 @@ 把 this time R0 store or Global_data address BL board_init_f_init_ Reserve @@ 调用 Board_init_f_init_reserve to initialize Global_data, which is the R0 parameter.

Note: The final global_data address is stored in the R9. 2. Global_data Memory Distribution

The memory distribution is as follows:
———————-config_sys_load_addr ————————— – High address

................................... Early malloc memory Pool

————————-early malloc memory Chi Ki address —————————

..... global_data. The area of the ". .....?????????????

—————-Global_data Base Address (R9), which is also the start of the stack ————-

............................................. Stack space

———————————— – End of stack —————————————-low address
Note: The final global_data address is stored in the R9. iv. global_data use mode 1, principle explanation

We have always emphasized that the Global_data address is stored in the R9.
So when we need to global_data, we can get the address directly from the R9 register.

a macro declare_global_data_ptr is defined in uboot so that we can get global_data more easily.
Defined as follows:
Arch/arm/include/asm/global_data.h

#define DECLARE_GLOBAL_DATA_PTR     Register volatile gd_t *gd asm ("R9")

Declare_global_data_ptr defines the gd_t *GD, and its address is the value in R9.
Once you have used the Declare_global_data_ptr declaration, you can use the GD variable directly, which is global_data. 2. Examples of Use

Declare_global_data_ptr defines the gd_t *GD, and its address is the value in R9.
Once you have used the Declare_global_data_ptr declaration, you can use the GD variable directly, which is global_data.
A simple example is as follows:
Common/board_r.c

DECLARE_GLOBAL_DATA_PTR//
defined by declare_global_data_ptr gd_t *gd
//equivalent to the following:
//#define Declare_global_ Data_ptr      Register volatile gd_t *gd asm ("R9")

static int initr_reloc (void)
{/
    * Tell Others:relocation Done */
    gd->flags |= Gd_flg_reloc | Gd_flg_full_malloc_init;
Direct use of the GD variable, also known as Uboot's global_data.

    return 0;
}

Global_data is relatively simple, it is not much to say.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.