modify all segment registers except ..(CS)

software | Windows |

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.

source :

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).

source :

software | GNU/Linux |

(gdb) info registers
rax            0xfffffffffffffdfc	-516
rbx            0x5dc	1500
rcx            0xffffffffffffffff	-1
rdx            0x5dc	1500
rsi            0x1	1
rdi            0x7fff6f396d50	140735059422544
rbp            0xb4a160	0xb4a160
rsp            0x7fff6f396d00	0x7fff6f396d00
r8             0x0	0
r9             0xffffffff	4294967295
r10            0x8	8
r11            0x246	582
r12            0x7fff6f396d50	140735059422544
r13            0x7fff6f396d60	140735059422560
r14            0x0	0
r15            0x1	1
rip            0x7fc4561ec0c8	0x7fc4561ec0c8
eflags         0x246	[ PF ZF IF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0

cs 0x33 51

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'.

source :

| 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) :,1