Start from machine_start

Source: Internet
Author: User

Note: The following content is based on linux-2.6.38 and mini6410 for learning.

Play or transplanted arm-Linux should know in the/ARCH/ARM directory there are a lot of directory related to the specific processor, of course, for 6410, the corresponding directory is the mach-s3c64xx, find the file mach-mini6410.c related to the specific board in it, that is, it. Whether it is to transplant to a new kernel or to learn a certain arm in depth, learning this file is essential. Most of the content in this file is the initialization of the structure of the platform device (such as the serial port, LCD, and NAND falsh). At the end of this file, there is a very important macro:

1 MACHINE_START(MINI6410, "MINI6410")2         /* Maintainer: Ben Dooks <ben-linux@fluff.org> */3         .boot_params    = S3C64XX_PA_SDRAM + 0x100,4 5         .init_irq       = s3c6410_init_irq,6         .map_io         = mini6410_map_io,7         .init_machine   = mini6410_machine_init,8         .timer          = &s3c24xx_timer,9 MACHINE_END

The macro mini6410 is defined in the/ARCH/ARM/tools/Mach-types file:

mini6410        MACH_MINI6410        MINI6410        2520

Machine_start is defined in arch/ARM/include/ASM/Mach/arch. H, as follows:

1 #define MACHINE_START(_type,_name)                      \2 static const struct machine_desc __mach_desc_##_type    \3  __used                                                 \4  __attribute__((__section__(".arch.info.init"))) = {    \5         .nr             = MACH_TYPE_##_type,            \6         .name           = _name,7 8 #define MACHINE_END                             \9 };

Oh, actually, it defines a struct variable of the struct machine_desc type. This struct also defines some other members. Next we will focus on it. the member init_machine, which is a function pointer with a value of mini6410_machine_init, which is also defined in the mach-mini6410.c. What is the content? Haha, because only the general process is provided here, and the specific content is not analyzed first. What concerns me most is where this struct variable is called to call its members and member functions? First, let's look at the setup_arch () function in/ARCH/ARM/kernel/setup. C:

 1 void __init setup_arch(char **cmdline_p) 2 { 3         struct tag *tags = (struct tag *)&init_tags; 4         struct machine_desc *mdesc; 5         char *from = default_command_line; 6  7         unwind_init(); 8  9         setup_processor();10         mdesc = setup_machine(machine_arch_type);11         machine_desc = mdesc;12         machine_name = mdesc->name;
......................

This function is called in the start_kernel () function of/init/Main. C. The setup_machine () function is used to find the variable of the struct machine_desc type we want, that is, to define the variable in the mach-mini6410.c. What is the value of machine_arch_type? Continue Viewing:

 1 #ifdef CONFIG_MACH_MINI6410 2 # ifdef machine_arch_type 3 #  undef machine_arch_type 4 #  define machine_arch_type     __machine_arch_type 5 # else 6 #  define machine_arch_type     MACH_TYPE_MINI6410 7 # endif 8 # define machine_is_mini6410()  (machine_arch_type == MACH_TYPE_MINI6410) 9 #else10 # define machine_is_mini6410()  (0)11 #endif

Row 3, mach_type_mini6410 macro:

#define MACH_TYPE_MINI6410             2520

That is to say, the machine_arch_type value is 2520. The setup_machine () function mainly calls the lookup_machine_type () function to find the corresponding type. This function is implemented through Assembly for efficiency reasons, no specific code is provided here.

At this point, we can see in/init/main. the start_kernel () function of C calls setup_arch () and finds the variable of the specific struct machine_desc type in setup_arch, but where can I use this variable to call Members or member functions? Continue searching. In setup. C, we can see such a function:

1 static int __init customize_machine(void)2 {3         /* customizes platform devices, or adds new ones */4         if (machine_desc->init_machine)5                 machine_desc->init_machine();6         return 0;7 }8 arch_initcall(customize_machine);

Finally, the member function init_machine is called here. However, it is not explicitly called, but stored in the macro arch_initcall to see how it is defined:

#define arch_initcall(fn)               __define_initcall("3",fn,3)

Look at the _ define_initcall macro:

1 #define __define_initcall(level,fn,id) \2         static initcall_t __initcall_##fn##id __used \3         __attribute__((__section__(".initcall" level ".init"))) = fn

Well, it is linked to the. initcall segment. Now let's take a look at the definition of initcall in the/ARCH/ARM/kernel/vmlinux. LDS link script:

 1 __initcall_start = .;  2 *(.initcallearly.init) __early_initcall_end = .;  3 *(.initcall0.init) *(.initcall0s.init)  4 *(.initcall1.init) *(.initcall1s.init)  5 *(.initcall2.init) *(.initcall2s.init)  6 *(.initcall3.init) *(.initcall3s.init)  7 *(.initcall4.init) *(.initcall4s.init)  8 *(.initcall5.init) *(.initcall5s.init)  9 *(.initcallrootfs.init) 10 *(.initcall6.init) *(.initcall6s.init) 11 *(.initcall7.init) *(.initcall7s.init) 12 __initcall_end = .;

We can see that customize_machine () is put in. initcall3.init. Where is the definition called? Well, it is called in a function named do_initcals () in/init/Main. C. Let's see it:

1 extern initcall_t __initcall_start[], __initcall_end[], __early_initcall_end[];2 3 static void __init do_initcalls(void)4 {5         initcall_t *fn;6 7         for (fn = __early_initcall_end; fn < __initcall_end; fn++)8                 do_one_initcall(*fn);9 }

You are familiar with the 1st rows. In the for loop, all functions starting from _ early_initcall_end to _ initcall_end are called sequentially. Customize_machine () is also called in the meantime.

Now, it's almost time to end. Finally, let's summarize the order of calling these functions:

Start_kernel () ---> setup_arch () ---> do_initcballs () ---> customize_machine () ---> mini6410_machine_init ()

 

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.