lcd-domains-arch.h 12.4 KB
Newer Older
1 2
#ifndef _ASM_X86_LCD_DOMAINS_ARCH_H
#define _ASM_X86_LCD_DOMAINS_ARCH_H
3

4
#include <asm/vmx.h>
5
#include <linux/spinlock.h>
6
#include <lcd-domains/utcb.h>
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/* ADDRESS SPACE TYPES ---------------------------------------- */

typedef struct { unsigned long gva } gva_t;
typedef struct { unsigned long hva } hva_t;
typedef struct { unsigned long gpa } gpa_t;
typedef struct { unsigned long hpa } hpa_t;

static inline gva_t __gva(unsigned long gva)
{
	return (gva_t){ gva };
}
static inline unsigned long gva_val(gva_t gva)
{
	return gva.gva;
}
static inline unsigned long * gva_ptr(gva_t * gva)
{
	return &(gva->gva);
}
static inline gva_t gva_add(gva_t gva, unsigned long off)
{
	return __gva(gva_val(gva) + off);
}
static inline hva_t __hva(unsigned long hva)
{
	return (hva_t){ hva };
}
static inline unsigned long hva_val(hva_t hva)
{
	return hva.hva;
}
static inline unsigned long * hva_ptr(hva_t * hva)
{
	return &(hva->hva);
}
static inline hva_t hva_add(hva_t hva, unsigned long off)
{
	return __hva(hva_val(hva) + off);
}
static inline gpa_t __gpa(unsigned long gpa)
{
	return (gpa_t){ gpa };
}
static inline unsigned long gpa_val(gpa_t gpa)
{
	return gpa.gpa;
}
static inline unsigned long * gpa_ptr(gpa_t * gpa)
{
	return &(gpa->gpa);
}
static inline gpa_t gpa_add(gpa_t gpa, unsigned long off)
{
	return __gpa(gpa_val(gpa) + off);
}
static inline hpa_t __hpa(unsigned long hpa)
{
	return (hpa_t){ hpa };
}
static inline unsigned long hpa_val(hpa_t hpa)
{
	return hpa.hpa;
}
static inline unsigned long * hpa_ptr(hpa_t * hpa)
{
	return &(hpa->hpa);
}
static inline hpa_t hpa_add(hpa_t hpa, unsigned long off)
{
	return __hpa(hpa_val(hpa) + off);
}
static inline hpa_t va2hpa(void *va)
{
	return (hpa_t){ __pa(va) };
}
static inline void * hpa2va(hpa_t hpa)
{
	return __va(hpa_val(hpa));
}
static inline hva_t hpa2hva(hpa_t hpa)
{
	return (hva_t){ (unsigned long)__va(hpa.hpa) };
}
static inline hpa_t hva2hpa(hva_t hva)
{
	return (hpa_t){ (unsigned long)__pa(hva2va(hva)) };
}
static inline void * hva2va(hva_t hva)
{
	return (void *)hva_val(hva);
}


/* LCD ARCH DATA STRUCTURES ---------------------------------------- */

103
struct lcd_arch_vmcs {
104 105 106 107 108
	u32 revision_id;
	u32 abort;
	char data[0];
};

109
#define LCD_ARCH_NUM_AUTOLOAD_MSRS 0
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
enum lcd_arch_reg {
	LCD_ARCH_REGS_RAX = 0,
	LCD_ARCH_REGS_RCX = 1,
	LCD_ARCH_REGS_RDX = 2,
	LCD_ARCH_REGS_RBX = 3,
	LCD_ARCH_REGS_RSP = 4,
	LCD_ARCH_REGS_RBP = 5,
	LCD_ARCH_REGS_RSI = 6,
	LCD_ARCH_REGS_RDI = 7,
	LCD_ARCH_REGS_R8 = 8,
	LCD_ARCH_REGS_R9 = 9,
	LCD_ARCH_REGS_R10 = 10,
	LCD_ARCH_REGS_R11 = 11,
	LCD_ARCH_REGS_R12 = 12,
	LCD_ARCH_REGS_R13 = 13,
	LCD_ARCH_REGS_R14 = 14,
	LCD_ARCH_REGS_R15 = 15,
	LCD_ARCH_REGS_RIP,
	LCD_ARCH_NUM_REGS
130 131
};

Charlie Jacobsen's avatar
Charlie Jacobsen committed
132 133
#define LCD_ARCH_EPT_WALK_LENGTH 4
#define LCD_ARCH_EPTP_WALK_SHIFT 3
134
#define LCD_ARCH_PTRS_PER_EPTE   (1 << 9)
Charlie Jacobsen's avatar
Charlie Jacobsen committed
135

136
struct lcd_arch_ept {
137
	spinlock_t lock;
138
	hpa_t root;
139 140
	unsigned long vmcs_ptr;
	bool access_dirty_enabled;
141 142
};

143
typedef unsigned long lcd_arch_epte_t;
Charlie Jacobsen's avatar
Charlie Jacobsen committed
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
struct lcd_arch_tss {
	/*
	 * Intel SDM V3 7.7
	 *
	 * Base TSS before I/O bitmap, etc.
	 */
	struct x86_hw_tss base_tss;
	/*
	 * I/O bitmap must be at least 8 bits to contain
	 * required 8 bits that are set.
	 *
	 * Intel SDM V1 16.5.2
	 */
	u8 io_bitmap[1];
} __attribute__((packed));

161
struct lcd_arch {
162 163 164
	/*
	 * Public Data
	 */
165
	struct {
166 167
		gva_t gv_fault_addr;
		gpa_t gp_fault_addr;
168
	} run_info;
169 170 171 172

	/*
	 * Private Data
	 */
173 174
	int cpu;
	int launched;
175
	int vpid;
176
	struct lcd_arch_vmcs *vmcs;
177

178
	struct lcd_arch_ept ept;
179 180
	struct desc_struct  *gdt;
	struct lcd_arch_tss *tss;
181
	struct lcd_utcb *utcb;
182 183 184 185 186 187 188 189 190

	u8  fail;
	u64 exit_reason;
	u64 exit_qualification;
	u32 idt_vectoring_info;
	u32 exit_intr_info;
	u32 error_code;
	u32 vec_no;
	u64 host_rsp;
191
	u64 regs[LCD_ARCH_NUM_REGS];
192 193 194 195 196
	u64 cr2;
	int shutdown;
	int ret_code;

	struct msr_autoload {
197 198 199
#if LCD_ARCH_NUM_AUTOLOAD_MSRS > 0
		struct vmx_msr_entry guest[LCD_ARCH_NUM_AUTOLOAD_MSRS];
		struct vmx_msr_entry host[LCD_ARCH_NUM_AUTOLOAD_MSRS];
200 201 202 203
#else
		struct vmx_msr_entry *guest;
		struct vmx_msr_entry *host;
#endif
204 205 206
	} msr_autoload;
};

207 208 209 210
/**
 * Initializes the arch-dependent code for LCD (detects required
 * features, turns on VMX on *all* cpu's).
 */
211
int lcd_arch_init(void);
212 213 214 215 216 217
/**
 * Turns off VMX on *all* cpu's and tears down arch-dependent code.
 * 
 * Important: All LCDs should be destroyed before calling this
 * routine (otherwise, memory will leak).
 */
218
void lcd_arch_exit(void);
219 220 221 222
/**
 * Creates the arch-dependent part of an LCD, and initializes 
 * the settings and most register values.
 */
223
struct lcd_arch *lcd_arch_create(void);
224 225 226 227
/**
 * Tears down arch-dep part of LCD. (If LCD is launched on
 * some cpu, it will become inactive.)
 */
228
void lcd_arch_destroy(struct lcd_arch *vcpu);
229 230 231 232 233
/**
 * Runs the LCD on the calling cpu. (If the LCD is active on
 * a different cpu, it will become inactive there.) Kernel
 * preemption is disabled while the LCD is launched, but
 * external interrupts are not disabled and will be handled.
234 235 236
 *
 * Unless the caller does otherwise, kernel preemption is
 * enabled before returning.
237 238 239
 *
 * Returns status code (e.g., LCD_ARCH_STATUS_PAGE_FAULT)
 * so that caller knows why lcd exited and can respond.
240 241 242 243 244 245 246
 */
int lcd_arch_run(struct lcd_arch *vcpu);

/**
 * Status codes for running LCDs.
 */
enum lcd_arch_status {
247
	LCD_ARCH_STATUS_PAGE_FAULT = 0,
248 249 250
	LCD_ARCH_STATUS_EXT_INTR   = 1,
	LCD_ARCH_STATUS_EPT_FAULT  = 2,
	LCD_ARCH_STATUS_CR3_ACCESS = 3,
251
	LCD_ARCH_STATUS_SYSCALL    = 4,
252
};
253

Charlie Jacobsen's avatar
Charlie Jacobsen committed
254
/**
255
 * Lookup ept entry for guest physical address a.
Charlie Jacobsen's avatar
Charlie Jacobsen committed
256 257 258 259
 *
 * Set create = 1 to allocate ept page table data structures
 * along the path as needed.
 */
260
int lcd_arch_ept_walk(struct lcd_arch *vcpu, gpa_t a, int create,
Charlie Jacobsen's avatar
Charlie Jacobsen committed
261 262 263 264
		lcd_arch_epte_t **epte_out);
/**
 * Set the guest physical => host physical mapping in the ept entry.
 */
265
void lcd_arch_ept_set(lcd_arch_epte_t *epte, hpa_t a);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
266 267 268
/**
 * Read the host physical address stored in epte.
 */
269
hpa_t lcd_arch_ept_hpa(lcd_arch_epte_t *epte);
270 271 272 273 274 275 276 277
/**
 * Clears guest physical => host physical mapping in the ept.
 *
 * (This is not going to free up potentially empty paging structures
 * higher up in the ept hierarchy; but, for now, unset is used when
 * we're tearing down the lcd -- due to an error -- so the paging
 * structures will be freed up when the ept is torn down.)
 */
278
int lcd_arch_ept_unset(lcd_arch_epte_t *epte);
279 280 281 282 283 284
/**
 * Simple routine combining ept walk and set.
 *
 * overwrite = 0  => do not overwrite if ept entry is already present
 * overwrite = 1  => overwrite any existing ept entry
 */
285 286
int lcd_arch_ept_map(struct lcd_arch *vcpu, gpa_t ga, hpa_t ha,
		int create, int overwrite);
287 288 289
/**
 * Maps 
 *
290
 *    ga_start --> ga_start + npages * PAGE_SIZE
291 292 293
 *
 * to
 *
294
 *    ha_start --> ha_start + npages * PAGE_SIZE
295 296 297
 *
 * in lcd's ept.
 */
298 299
int lcd_arch_ept_map_range(struct lcd_arch *lcd, gpa_t ga_start, 
			hpa_t ha_start, unsigned long npages);
300 301 302
/**
 * Simple routine combining ept walk and unset.
 */
303
int lcd_arch_ept_unmap(struct lcd_arch *vcpu, gpa_t a);
304 305 306
/**
 * Unmaps 
 *
307
 *    ga_start --> ga_start + npages * PAGE_SIZE
308 309 310
 *
 * in lcd's ept.
 */
311 312
int lcd_arch_ept_unmap_range(struct lcd_arch *lcd, gpa_t ga_start, 
			unsigned long npages);
313 314 315
/**
 * Simple routine combinding ept walk and get.
 */
316
int lcd_arch_ept_gpa_to_hpa(struct lcd_arch *vcpu, gpa_t ga, hpa_t *ha_out);
317 318
/**
 * Set the lcd's program counter to the guest physical address
319
 * a.
320
 */
321
int lcd_arch_set_pc(struct lcd_arch *vcpu, gpa_t a);
322 323
/**
 * Set the lcd's gva root pointer (for x86, %cr3) to the
324
 * guest physical address a.
325
 */
326
int lcd_arch_set_gva_root(struct lcd_arch *vcpu, gpa_t a);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
327

328 329 330 331 332
/*
 * GDT Layout
 * ==========
 * 0 = NULL
 * 1 = Code segment
333 334
 * 2 = Data segment  (%fs, default not present)
 * 3 = Data segment  (%gs, default not present)
335 336 337 338 339 340
 * 4 = Task segment
 *
 * See Intel SDM V3 26.3.1.2, 26.3.1.3 for register requirements.
 * See Intel SDM V3 3.4.2, 3.4.3 for segment register layout
 * See Intel SDM V3 2.4.1 - 2.4.4 for gdtr, ldtr, idtr, tr
 */
341
#define LCD_ARCH_FS_BASE     __gpa(0UL);
342
#define LCD_ARCH_FS_LIMIT    0xFFFFFFFF
343
#define LCD_ARCH_GS_BASE     __gpa(0UL);
344
#define LCD_ARCH_GS_LIMIT    0xFFFFFFFF
345
#define LCD_ARCH_GDTR_BASE   __gpa(1UL << PAGE_SHIFT);
346
#define LCD_ARCH_GDTR_LIMIT  ((u32)~(PAGE_SIZE - 1))
347
#define LCD_ARCH_TSS_BASE    __gpa(2UL << PAGE_SHIFT);
348 349
/* tss base + limit = address of last byte in tss, hence -1 */
#define LCD_ARCH_TSS_LIMIT   (sizeof(struct lcd_arch_tss) - 1)
350
#define LCD_ARCH_IDTR_BASE   __gpa(0UL);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
#define LCD_ARCH_IDTR_LIMIT  0x0 /* no idt right now */

#define LCD_ARCH_CS_SELECTOR   (1 << 3)
#define LCD_ARCH_FS_SELECTOR   (2 << 3)
#define LCD_ARCH_GS_SELECTOR   (3 << 3)
#define LCD_ARCH_TR_SELECTOR   (4 << 3) /* TI must be 0 */
#define LCD_ARCH_LDTR_SELECTOR (0 << 3) /* unusable */

/*
 * Guest Physical Memory Layout
 * ============================
 *
 *                         +---------------------------+ 0xFFFF FFFF FFFF FFFF
 *                         |                           |
 *                         :                           :
 *                         :      Free / Unmapped      :
 *                         :                           :
 *                         |                           |
 * LCD_ARCH_STACK_TOP,---> +---------------------------+ 0x0000 0000 0000 4000
 * LCD_ARCH_FREE           |                           |
 *                         |          Stack            |
 *                         :       (grows down)        : (4 KBs)
 *                         :                           :
 *                         |                           |
375 376
 *                         |   User Thread Ctrl Block  |
 * LCD_ARCH_UTCB---------> +---------------------------+ 0x0000 0000 0000 3000
377 378 379 380 381
 *                         |           TSS             |
 *                         |    only sizeof(tss) is    | (4 KBs)
 *                         |           used            |
 * LCD_ARCH_TSS_BASE-----> +---------------------------+ 0x0000 0000 0000 2000
 *                         |           GDT             | (4 KBs)
382
 * LCD_ARCH_GDTR_BASE----> +---------------------------+ 0x0000 0000 0000 1000
383 384 385 386
 *                         |         Reserved          |
 *                         |       (not mapped)        | (4 KBs)
 *                         +---------------------------+ 0x0000 0000 0000 0000
 */
387 388
#define LCD_ARCH_UTCB        __gpa(3UL << PAGE_SHIFT);
#define LCD_ARCH_STACK_TOP   __gpa(4UL << PAGE_SHIFT);
389
#define LCD_ARCH_FREE        LCD_ARCH_STACK_TOP
Charlie Jacobsen's avatar
Charlie Jacobsen committed
390

391 392 393 394 395
/*
 * Accessor Macro for syscalls
 * ===========================
 */
#define LCD_ARCH_GET_SYSCALL_NUM(vcpu) (vcpu->regs[LCD_ARCH_REGS_RAX])
396

397 398 399 400 401 402
/*
 * Accessor Macro for utcb
 * =======================
 */
#define LCD_ARCH_GET_UTCB(vcpu) (vcpu->utcb);

403 404 405 406 407 408 409 410
/*
 * Accessor Macros for IPC
 * =======================
 *
 * Based on x86 seL4 message register design.
 *
 * See seL4 manual, 4.1.
 */
411
#define LCD_ARCH_GET_CAP_REG(vcpu) (vcpu->regs[LCD_ARCH_REGS_RBX])
412 413 414
#define LCD_ARCH_GET_BDG_REG(vcpu) (vcpu->regs[LCD_ARCH_REGS_RBX])
#define LCD_ARCH_GET_TAG_REG(vcpu) (vcpu->regs[LCD_ARCH_REGS_RSI])
#define LCD_ARCH_GET_MSG_REG(vcpu, idx) (__lcd_arch_get_msg_reg(vcpu, idx))
415 416
static inline u64 __lcd_arch_get_msg_reg(struct lcd_arch *vcpu, 
					unsigned int idx)
417 418 419 420 421 422
{
	/*
	 * Message regs 0 and 1 are fast (use machine registers)
	 *
	 * Message regs 2, ... always use the mr's in struct lcd_ipc_regs.
	 *
423
	 * (The first two mr's in utcb are reserved for
424
	 * mr's 0 and 1. If the caller wishes to explicitly use those mr's,
425
	 * they should do so by manually accessing the mr's in utcb.)
426 427
	 */
	if (idx == 0)
428
		return vcpu->regs[LCD_ARCH_REGS_RDI];
429
	else if (idx == 1)
430
		return vcpu->regs[LCD_ARCH_REGS_RBP];
431
	else
432
		return vcpu->utcb->ipc.mr[idx];
433 434 435
}

#define LCD_ARCH_SET_CAP_REG(vcpu, val) ({                    \
436
			vcpu->regs[LCD_ARCH_REGS_RBX] = val;  \
437 438 439 440 441 442 443 444 445 446
		})
#define LCD_ARCH_SET_BDG_REG(vcpu, val) ({                    \
			vcpu->regs[LCD_ARCH_REGS_RBX] = val;  \
		})
#define LCD_ARCH_SET_TAG_REG(vcpu, val) ({                    \
			vcpu->regs[LCD_ARCH_REGS_RSI] = val;  \
		})
#define LCD_ARCH_SET_MSG_REG(vcpu, idx, val) ({                 \
			__lcd_arch_set_msg_reg(vcpu, val, idx);	\
		})
447 448
static inline void __lcd_arch_set_msg_reg(struct lcd_arch *vcpu, 
					unsigned int idx, u64 val)
449 450 451 452 453 454
{
	/*
	 * Message regs 0 and 1 are fast (use machine registers)
	 *
	 * Message regs 2, ... always use the mr's in struct lcd_ipc_regs.
	 *
455
	 * (The first two mr's in utcb are reserved for
456
	 * mr's 0 and 1. If the caller wishes to explicitly use those mr's,
457
	 * they should do so by manually accessing the mr's in utcb.)
458 459
	 */
	if (idx == 0)
460
		vcpu->regs[LCD_ARCH_REGS_RDI] = val;
461
	else if (idx == 1)
462
		vcpu->regs[LCD_ARCH_REGS_RBP] = val;
463
	else
464
		vcpu->utcb->ipc.mr[idx] = val;
465 466
}

467
#endif  /* _ASM_X86_LCD_DOMAINS_ARCH_H */