Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel assembly and logic

My question is somewhat weird but I will do my best to explain.

Looking at the languages the linux kernel has, I got C and assembly even though I read a text that said [quote] Second iteration of Unix is written completely in C [/quote]

I thought that was misleading but when I said that kernel has assembly code I got 2 questions of the start

  1. What assembly files are in the kernel and what's their use?
  2. Assembly is architecture dependant so how can linux be installed on more than one CPU architecture

And if linux kernel is truly written completely in C than how can it get GCC needed for compiling?

I did a complete find / -name *.s and just got one assembly file (asm-offset.s) somewhere in the /usr/src/linux-headers-`uname -r/

Somehow I don't think that is helping with the GCC working, so how can linux work without assembly or if it uses assembly where is it and how can it be stable when it depends on the arch.

Thanks in advance

like image 668
daniels_pa Avatar asked Mar 02 '14 01:03

daniels_pa


2 Answers

1. Why assembly is used?

Because there are certain things then can be done only in assembly and because assembly results in a faster code. For eg, "you can get access to unusual programming modes of your processor (e.g. 16 bit mode to interface startup, firmware, or legacy code on Intel PCs)". Read here for more reasons.

2. What assembly file are used?

From: https://www.kernel.org/doc/Documentation/arm/README

"The initial entry into the kernel is via head.S, which uses machine independent code. The machine is selected by the value of 'r1' on entry, which must be kept unique."

From https://www.ibm.com/developerworks/library/l-linuxboot/

"When the bzImage (for an i386 image) is invoked, you begin at ./arch/i386/boot/head.S in the start assembly routine (see Figure 3 for the major flow). This routine does some basic hardware setup and invokes the startup_32 routine in ./arch/i386/boot/compressed/head.S. This routine sets up a basic environment (stack, etc.) and clears the Block Started by Symbol (BSS). The kernel is then decompressed through a call to a C function called decompress_kernel (located in ./arch/i386/boot/compressed/misc.c). When the kernel is decompressed into memory, it is called. This is yet another startup_32 function, but this function is in ./arch/i386/kernel/head.S."

Apart from these assembly files, lot of linux kernel code has usage of inline assembly.

3. Architecture dependence?

And you are right about it being architecture dependent, that's why the linux kernel code is ported to different architecture.

  • Linux porting guide
  • List of supported arch
like image 180
brokenfoot Avatar answered Oct 01 '22 19:10

brokenfoot


Things written mainly in assembly in Linux:

  • Boot code: boots up the machine and sets it up in a state in which it can start executing C code (e.g: on some processors you may need to manually initialize caches and TLBs, on x86 you have to switch to protected mode, ...)
  • Interrupts/Exceptions/Traps entry points/returns: there you need to do very processor-specific things, e.g: saving registers and reenabling interrupts, and eventually restoring registers and properly returning to user mode. Some exceptions may be handled entirely in assembly.
  • Instruction emulation: some CPU models may not support certain instructions, may not support unaligned data access, or may not have an FPU. An option is using emulation when getting the corresponding exception.
  • VDSO: the VDSO is a virtual library that the kernel maps into userspace. It allows e.g: selecting the optimal syscall sequence for the current CPU (on x86 use sysenter/syscall instead of int 0x80 if available), and implementing certain system calls without requiring a context switch (e.g: gettimeofday()).
  • Atomic operations and locks: Maybe in a future some of these could be written using C11 support for atomic operations.
  • Copying memory from/to user mode: Besides using an optimized copy, these check for out-of-bounds access.
  • Optimized routines: the kernel has optimized version of some routines, e.g: crypto routines, memset, clear_page, csum_copy (checksum and copy to another place IP data in one pass), ...
  • Support for suspend/resume and other ACPI/EFI/firmware thingies
  • BPF JIT: newer kernels include a JIT compiler for BPF expressions (used for example by tcpdump, secmode mode 2, ...)
  • ...

To support different architectures, Linux has assembly code (re-)written for each architecture it supports (and sometimes, there are several implementations of some code for different platforms using the same CPU architecture). Just look at all the subdirectories under arch/

like image 25
ninjalj Avatar answered Oct 01 '22 19:10

ninjalj