1/*-
2 * Copyright (c) 2014, 2015 Antti Kantee.  All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26/*
27 * We need to provide a segment for gs for TLS to work, so we need to
28 * install our own gdt.  Hence, all this complexity follows.
29 */
30
31#include <bmk-core/platform.h>
32#include <bmk-core/string.h>
33
34#include <mini-os/hypervisor.h>
35#include <mini-os/mm.h>
36#include <mini-os/os.h>
37
38#if defined(__i386__)
39
40/*
41 * i386 MD desciptor, assimilated from NetBSD sys/arch/i386/include/segments.h
42 */
43
44struct segment_descriptor {
45        unsigned sd_lolimit:16;
46        unsigned sd_lobase:24;
47        unsigned sd_type:5;
48        unsigned sd_dpl:2;
49        unsigned sd_p:1;
50        unsigned sd_hilimit:4;
51        unsigned sd_xx:2;
52        unsigned sd_def32:1;
53        unsigned sd_gran:1;
54        unsigned sd_hibase:8;
55} __attribute__((__packed__));
56
57#define SDT_MEMRWA	19	/* memory read write accessed */
58#define SDT_MEMERA	27	/* memory execute read accessed */
59
60#define SEGMENT_CODE	(__KERNEL_CS>>3)
61#define SEGMENT_DATA	(__KERNEL_DS>>3)
62#define SEGMENT_GS	(__KERNEL_GS>>3)
63
64/*
65 * Make sure the gdt takes up exactly 1 page since we need(?) to
66 * map it read-only before we pass it up to Xen.
67 */
68#define GDTCOUNT (PAGE_SIZE/sizeof(struct segment_descriptor))
69static struct segment_descriptor gdt[GDTCOUNT]
70    __attribute__((aligned(PAGE_SIZE)));
71static unsigned long long gs_pa;
72
73static union {
74	struct segment_descriptor gs;
75	unsigned long long gs_raw;
76} gs;
77
78void _minios_entry_load_segmentregs(void);
79
80static void
81fillsegment(struct segment_descriptor *sd, int type)
82{
83
84	sd->sd_lobase = 0;
85	sd->sd_hibase = 0;
86
87	sd->sd_lolimit = 0xffff;
88	sd->sd_hilimit = 0xf;
89
90	sd->sd_type = type;
91
92	sd->sd_dpl = 1;
93	sd->sd_p = 1;
94	sd->sd_xx = 0;
95	sd->sd_def32 = 1;
96	sd->sd_gran = 1;
97}
98
99void
100tlsswitch32(unsigned long p)
101{
102
103	gs.gs.sd_lobase = p & 0xffffff;
104	gs.gs.sd_hibase = (p >> 24) & 0xff;
105	if (HYPERVISOR_update_descriptor(gs_pa, gs.gs_raw))
106		bmk_platform_halt("updating TLS descriptor failed");
107
108	/* reload gs register to finalize change */
109	__asm__ __volatile__("mov %0, %%gs" :: "r"(__KERNEL_GS));
110}
111
112void
113gdtinit32(void)
114{
115	pte_t pte;
116	unsigned long frames[1];
117	int error;
118
119	fillsegment(&gdt[SEGMENT_CODE], SDT_MEMERA);
120	fillsegment(&gdt[SEGMENT_DATA], SDT_MEMRWA);
121	fillsegment(&gdt[SEGMENT_GS], SDT_MEMRWA);
122
123	pte = __pte((virt_to_mach(&gdt)) | L1_PROT_RO);
124	if (HYPERVISOR_update_va_mapping((unsigned long)&gdt, pte, UVMF_INVLPG))
125		bmk_platform_halt("make gdt r/o");
126
127	frames[0] = virt_to_mfn(&gdt);
128	if ((error = HYPERVISOR_set_gdt(frames, 512) != 0)) {
129		bmk_platform_halt("set_gdt");
130	}
131	_minios_entry_load_segmentregs();
132
133	/* cache things for the TLS switch */
134	bmk_memcpy(&gs, &gdt[SEGMENT_GS], sizeof(gs));
135	gs_pa = virt_to_mach(&gdt[SEGMENT_GS]);
136}
137#endif /* __i386__ */
138