I386_GET_LDT(2)

πŸ’– I386_GET_LDT πŸ’–

okay bestie, gather round, because we're about to talk about the i386_get_ldt(2) and i386_set_ldt(2) system calls β€” they live in the System Calls Manual section 2, and they're the ones that manage i386 per-process Local Descriptor Table entries. lowkey iconic, highkey terrifying, let's go πŸ¦‹


✨ NAME ✨

i386_get_ldt, i386_set_ldt β€” manage i386 per-process Local Descriptor Table entries πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) πŸŽ€


🌸 SYNOPSIS 🌸

#include <machine/segments.h>
#include <machine/sysarch.h>

int
i386_get_ldt(int start_sel, union descriptor *descs, int num_sels);

int
i386_set_ldt(int start_sel, union descriptor *descs, int num_sels);

like, those two includes are NOT optional, ok? and yeah, they both take the exact same three args, no cap 🫢


πŸ’« DESCRIPTION πŸ’«

so the tea is this: i386_get_ldt() returns a list of the i386 descriptors sitting in the current process' LDT. meanwhile i386_set_ldt() sets a list of i386 descriptors in the current process' LDT. two sides of the same coin, fr fr πŸ’–

for both routines, start_sel says which index of the selector in the LDT to start at, and descs points to an array of num_sels descriptors to be set or returned. simple as that ✨

each entry in the descs array can be either a segment_descriptor or a gate_descriptor, and those are defined in <i386/segments.h>. now listen β€” these structures are defined by the architecture as disjoint bit-fields, so you have to be so careful when you're constructing them. like, care must be taken. genuinely. do not freestyle this πŸ™ˆ

okay and here are the three special cases, and they're giving main character energy:

  • if start_sel is LDT_AUTO_ALLOC, num_sels is 1 and the descriptor pointed to by descs is legal, then i386_set_ldt() will allocate a descriptor and return its selector number πŸ‘‘
  • if num_descs is 1, start_sels is valid, and descs is NULL, then i386_set_ldt() will free that descriptor (making it available to be reallocated again later) 🧚
  • if num_descs is 0, start_sels is 0 and descs is NULL then, as a special case, i386_set_ldt() will free all descriptors πŸ’€

⭐ RETURN VALUES ⭐

upon successful completion, i386_get_ldt() returns the number of descriptors currently in the LDT. cute, right?

the i386_set_ldt() system call returns the first selector set on success. and if the kernel allocated a descriptor in the LDT, the allocated index is returned πŸŽ€

otherwise? a value of -1 is returned and the global variable errno is set to indicate the error. not the vibe. we don't love that for us 😭


πŸ”₯ ERRORS πŸ”₯

the i386_get_ldt() and i386_set_ldt() system calls will fail if:

  • [EINVAL] β€” an inappropriate value was used for start_sel or num_sels. ma'am. what did you type. πŸ’…
  • [EACCES] β€” the caller attempted to use a descriptor that would circumvent protection or cause a failure. bestie no, the kernel said absolutely not 🚫

🌈 SEE ALSO 🌈

  • i386 Microprocessor Programmer's Reference Manual, Intel πŸ“–

iykyk, that's the source material right there ✨


⚠️ WARNING ⚠️

You can really hose your process using this.

like I cannot stress this enough. genuinely. this is the scary part. be careful, bestie πŸ˜­πŸ™ˆ


and that's the whole page, periodt πŸ’– this one came from the Debian crew, dated October 14, 2006, and it's the I386_GET_LDT(2) page β€” go forth, manage your descriptor tables responsibly, and don't hose anything πŸ«Άβœ¨πŸ’…

πŸ“¦ where this page comes from