In computing, a code segment, also known as a text segment or simply
as text, is a phrase used to refer to a portion of memory or of an
object file that contains executable instructions.
Note that code may always modify all segment registers except CS (the
code segment). This is because the current privilege level (CPL) of the
processor is stored in the lower 2 bits of the CS register. The only way
to raise the processor privilege level (and reload CS) is through the
lcall (far call) and int (interrupt) instructions. Similarly, the only way to
lower the privilege level (and reload CS) is through lret (far return) and
iret (interrupt return).
Register operands are always prefixes with `%'. The 80386 registers
consist of the 6 section registers `%cs' (code section), `%ds' (data
section), `%ss' (stack section), `%es', `%fs', and `%gs'.
| Related Discussion |
CS Register Setting by VnutZ :: NR10 :: Show
The article correctly mentions the importance of setting up segment
registers, yet like most neglects to set up CS (which is 0×0000). This is
one nasty latent bug that shows itself as soon as you try doing indirect
jumps. So if you want to use something like threaded code in your first
stage bootloader set CS by "jmp 0×07c0:foo" first.
You’re right – it would have been "good practice" to set the CS register.
However, the CS register is already correctly set by the BIOS. If it
were not set … a computer would never boot up! CS (code segment)
and IP (instruction pointer) are both set to point directly at 0000:7C00
which is where the BIOS loads the bootsector into.
| Variation |
The way to execute user processes in kernel mode in AMD64 is almost
the same as it is in IA-32. To execute user processes in kernel mode,
the only thing KML does is launch user processes with the CS segment
register, which points to the kernel code segment instead of user code
In AMD64 CPUs, the privilege level of running programs is determined by
the privilege level of their code segment. This is almost the same as in
IA-32 CPUs; the only difference is the segmentation memory system is
degenerated in AMD64. Although segment registers still are used in 64
-bit mode of AMD64, the only segment that the segment registers can
use is the 16 EB flat segment. Thus, the role of the segment
descriptors is simply to specify privilege levels. Therefore, only four
segments—kernel code segment, kernel data segment, user code
segment—exist in 64-bit mode.
source and link(s) :