kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Are there global constructors in the Linux kernel?
@ 2019-10-09  3:44 CRISTIAN ANDRES VARGAS GONZALEZ
  2019-10-09  6:54 ` Giovanni Mascellani
  2019-10-09  8:54 ` Konstantin Andreev
  0 siblings, 2 replies; 3+ messages in thread
From: CRISTIAN ANDRES VARGAS GONZALEZ @ 2019-10-09  3:44 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 854 bytes --]

Good day

 I have been doing a little kernel following some repositories of GitHub,
videos and wikis, and I have found a topic that leaves me with doubts, the
theme is the global constructors.

Suppose I write a program in c, for the user space, for my code to run I
must have the main function
int main() { /*Code*/}

The gcc compiler searches for this function to run my program, but before
that happens, I can use the global constructors and write a code that runs
before the main function, but in the case of writing a kernel, why do I
need to run lines of code before the kernel is executed ?, I understand
that when writing a kernel there is no main function and that it is
disabled to indicate it to the compiler. In the Linux kernel are there
global constructors? If so, what things are executed before starting the
kernel?
att: cristian vargas.

[-- Attachment #1.2: Type: text/html, Size: 1364 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Are there global constructors in the Linux kernel?
  2019-10-09  3:44 Are there global constructors in the Linux kernel? CRISTIAN ANDRES VARGAS GONZALEZ
@ 2019-10-09  6:54 ` Giovanni Mascellani
  2019-10-09  8:54 ` Konstantin Andreev
  1 sibling, 0 replies; 3+ messages in thread
From: Giovanni Mascellani @ 2019-10-09  6:54 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1.1: Type: text/plain, Size: 2099 bytes --]

Hi,

[sending again after having replied just to the OP by mistake, sorry!]

Il 09/10/19 05:44, CRISTIAN ANDRES VARGAS GONZALEZ ha scritto:
> The gcc compiler searches for this function to run my program, but
> before that happens, I can use the global constructors and write a code
> that runs before the main function, but in the case of writing a kernel,
> why do I need to run lines of code before the kernel is executed ?, I
> understand that when writing a kernel there is no main function and that
> it is disabled to indicate it to the compiler. In the Linux kernel are
> there global constructors? If so, what things are executed before
> starting the kernel?

I am not a Linux expert, but I think I know enough on this specific
topic to give at least a partial answer: the global constructors feature
depends on the initialization code in the C runtime environment, which
depends on the operating system. In the case of the kernel, there is not
another operating system around, so everything that must happen at
initialization time must be done by the kernel itself, including setting
up the stack, zeroing uninitialized data and calling whatever you want
to be called. There cannot be any automatic way to do these things,
because there is nobody except the kernel itself who can do that.

This book has a nice and rather detailed write-up on what happens when
Linux boots (at least on x86_64):

  https://0xax.gitbooks.io/linux-insides/

You will see that nothing is called "automatically" like for global
constructors: everything is called explicitly and at the right moment.
This is also necessary because early kernel initialization routines
cannot assume that the system is in a consistent state, since their job
is to bring it to be. Therefore you must control precisely what must
happen when.

Hope this helps!

Giovanni.
-- 
Giovanni Mascellani <g.mascellani@gmail.com>
Postdoc researcher - Université Libre de Bruxelles





-- 
Giovanni Mascellani <g.mascellani@gmail.com>
Postdoc researcher - Université Libre de Bruxelles


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Are there global constructors in the Linux kernel?
  2019-10-09  3:44 Are there global constructors in the Linux kernel? CRISTIAN ANDRES VARGAS GONZALEZ
  2019-10-09  6:54 ` Giovanni Mascellani
@ 2019-10-09  8:54 ` Konstantin Andreev
  1 sibling, 0 replies; 3+ messages in thread
From: Konstantin Andreev @ 2019-10-09  8:54 UTC (permalink / raw)
  To: CRISTIAN ANDRES VARGAS GONZALEZ, kernelnewbies

Hi, Cristian. There are, a kinda of ...

kernel/taskstats.c:
| late_initcall(taskstats_init);

kernel/rcu/update.c:
| early_initcall(check_cpu_stall_init);

There is a whole set of macros STAGE_initcall() in include/linux/init.h

Arguments to these macros invokations are accounted and called by kernel initialization code at strictly defined stages of initialization. See https://0xax.gitbooks.io/linux-insides/content/Concepts/linux-cpu-3.html

The only thing that is run before kernel is <a series of> platform-specific boot loaders, maybe under hypervisor control. Compiler has no power to insert something in between.

Regards, Konstantin.

CRISTIAN ANDRES VARGAS GONZALEZ, 09 Oct 2019 06:44 MSK:
>
> Good day
>
>   I have been doing a little kernel following some repositories of GitHub, videos and wikis, and I have found a topic that leaves me with doubts, the theme is the global constructors.
>
> Suppose I write a program in c, for the user space, for my code to run I must have the main function
> int main() { /*Code*/}
>
> The gcc compiler searches for this function to run my program, but before that happens, I can use the global constructors and write a code that runs before the main function, but in the case of writing a kernel, why do I need to run lines of code before the kernel is executed ?, I understand that when writing a kernel there is no main function and that it is disabled to indicate it to the compiler. In the Linux kernel are there global constructors? If so, what things are executed before starting the kernel?
> att: cristian vargas.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-10-09  8:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-09  3:44 Are there global constructors in the Linux kernel? CRISTIAN ANDRES VARGAS GONZALEZ
2019-10-09  6:54 ` Giovanni Mascellani
2019-10-09  8:54 ` Konstantin Andreev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).