#include <linux/errno.h>
#include <linux/gfp.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/syscalls.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <asm/ldt.h>
#include <asm/tlb.h>
#include <asm/desc.h>
#include <asm/mmu_context.h>
#include <asm/pgtable_areas.h>
#include <xen/xen.h>
#define LDT_SLOT_STRIDE …
static inline void *ldt_slot_va(int slot)
{ … }
void load_mm_ldt(struct mm_struct *mm)
{ … }
void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
{ … }
static void refresh_ldt_segments(void)
{ … }
static void flush_ldt(void *__mm)
{ … }
static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
{ … }
#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
static void do_sanity_check(struct mm_struct *mm,
bool had_kernel_mapping,
bool had_user_mapping)
{ … }
#ifdef CONFIG_X86_PAE
static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
{
p4d_t *p4d;
pud_t *pud;
if (pgd->pgd == 0)
return NULL;
p4d = p4d_offset(pgd, va);
if (p4d_none(*p4d))
return NULL;
pud = pud_offset(p4d, va);
if (pud_none(*pud))
return NULL;
return pmd_offset(pud, va);
}
static void map_ldt_struct_to_user(struct mm_struct *mm)
{
pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
pmd_t *k_pmd, *u_pmd;
k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
set_pmd(u_pmd, *k_pmd);
}
static void sanity_check_ldt_mapping(struct mm_struct *mm)
{
pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
bool had_kernel, had_user;
pmd_t *k_pmd, *u_pmd;
k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
had_kernel = (k_pmd->pmd != 0);
had_user = (u_pmd->pmd != 0);
do_sanity_check(mm, had_kernel, had_user);
}
#else
static void map_ldt_struct_to_user(struct mm_struct *mm)
{ … }
static void sanity_check_ldt_mapping(struct mm_struct *mm)
{ … }
#endif
static int
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
{ … }
static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
{ … }
#else
static int
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
{
return 0;
}
static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
{
}
#endif
static void free_ldt_pgtables(struct mm_struct *mm)
{ … }
static void finalize_ldt_struct(struct ldt_struct *ldt)
{ … }
static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
{ … }
static void free_ldt_struct(struct ldt_struct *ldt)
{ … }
int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
{ … }
void destroy_context_ldt(struct mm_struct *mm)
{ … }
void ldt_arch_exit_mmap(struct mm_struct *mm)
{ … }
static int read_ldt(void __user *ptr, unsigned long bytecount)
{ … }
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
{ … }
static bool allow_16bit_segments(void)
{ … }
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
{ … }
SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
unsigned long , bytecount)
{ … }