[f52e54da] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[f52e54da] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_ia32_ddi
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[f52e54da] | 35 | #include <ddi/ddi.h>
|
---|
[2382d09] | 36 | #include <arch/ddi/ddi.h>
|
---|
[63e27ef] | 37 | #include <assert.h>
|
---|
[f52e54da] | 38 | #include <proc/task.h>
|
---|
[83dab11] | 39 | #include <stddef.h>
|
---|
[73e9b49] | 40 | #include <adt/bitmap.h>
|
---|
| 41 | #include <mm/slab.h>
|
---|
| 42 | #include <arch/pm.h>
|
---|
| 43 | #include <errno.h>
|
---|
[c7c0b89b] | 44 | #include <arch/cpu.h>
|
---|
[2382d09] | 45 | #include <cpu.h>
|
---|
| 46 | #include <arch.h>
|
---|
[ea199e5] | 47 | #include <align.h>
|
---|
[f52e54da] | 48 |
|
---|
[2382d09] | 49 | /** Install I/O Permission bitmap.
|
---|
| 50 | *
|
---|
| 51 | * Current task's I/O permission bitmap, if any, is installed
|
---|
| 52 | * in the current CPU's TSS.
|
---|
| 53 | *
|
---|
| 54 | * Interrupts must be disabled prior this call.
|
---|
[da1bafb] | 55 | *
|
---|
[2382d09] | 56 | */
|
---|
| 57 | void io_perm_bitmap_install(void)
|
---|
| 58 | {
|
---|
| 59 | /* First, copy the I/O Permission Bitmap. */
|
---|
[da1bafb] | 60 | irq_spinlock_lock(&TASK->lock, false);
|
---|
[a35b458] | 61 |
|
---|
[da1bafb] | 62 | size_t ver = TASK->arch.iomapver;
|
---|
[7de18418] | 63 | size_t elements = TASK->arch.iomap.elements;
|
---|
[a35b458] | 64 |
|
---|
[7de18418] | 65 | if (elements > 0) {
|
---|
[63e27ef] | 66 | assert(TASK->arch.iomap.bits);
|
---|
[a35b458] | 67 |
|
---|
[da1bafb] | 68 | bitmap_t iomap;
|
---|
[c5396c1] | 69 | bitmap_initialize(&iomap, TSS_IOMAP_SIZE * 8,
|
---|
[7de18418] | 70 | CPU->arch.tss->iomap);
|
---|
| 71 | bitmap_copy(&iomap, &TASK->arch.iomap, elements);
|
---|
[a35b458] | 72 |
|
---|
[e9e5b9ab] | 73 | /*
|
---|
| 74 | * Set the trailing bits in the last byte of the map to disable
|
---|
| 75 | * I/O access.
|
---|
| 76 | */
|
---|
[7de18418] | 77 | bitmap_set_range(&iomap, elements,
|
---|
| 78 | ALIGN_UP(elements, 8) - elements);
|
---|
[a35b458] | 79 |
|
---|
[2382d09] | 80 | /*
|
---|
[25b9e2c] | 81 | * It is safe to set the trailing eight bits because of the
|
---|
| 82 | * extra convenience byte in TSS_IOMAP_SIZE.
|
---|
[2382d09] | 83 | */
|
---|
[7de18418] | 84 | bitmap_set_range(&iomap, ALIGN_UP(elements, 8), 8);
|
---|
[2382d09] | 85 | }
|
---|
[a35b458] | 86 |
|
---|
[da1bafb] | 87 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
[a35b458] | 88 |
|
---|
[ea199e5] | 89 | /*
|
---|
| 90 | * Second, adjust TSS segment limit.
|
---|
| 91 | * Take the extra ending byte with all bits set into account.
|
---|
| 92 | */
|
---|
[da1bafb] | 93 | ptr_16_32_t cpugdtr;
|
---|
[2382d09] | 94 | gdtr_store(&cpugdtr);
|
---|
[a35b458] | 95 |
|
---|
[da1bafb] | 96 | descriptor_t *gdt_p = (descriptor_t *) cpugdtr.base;
|
---|
[c5396c1] | 97 | size_t size = bitmap_size(elements);
|
---|
[7de18418] | 98 | gdt_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE + size);
|
---|
[2382d09] | 99 | gdtr_load(&cpugdtr);
|
---|
[a35b458] | 100 |
|
---|
[2382d09] | 101 | /*
|
---|
| 102 | * Before we load new TSS limit, the current TSS descriptor
|
---|
| 103 | * type must be changed to describe inactive TSS.
|
---|
| 104 | */
|
---|
| 105 | gdt_p[TSS_DES].access = AR_PRESENT | AR_TSS | DPL_KERNEL;
|
---|
[1d3d2cf] | 106 | tr_load(GDT_SELECTOR(TSS_DES));
|
---|
[a35b458] | 107 |
|
---|
[2382d09] | 108 | /*
|
---|
| 109 | * Update the generation count so that faults caused by
|
---|
| 110 | * early accesses can be serviced.
|
---|
| 111 | */
|
---|
| 112 | CPU->arch.iomapver_copy = ver;
|
---|
| 113 | }
|
---|
[b45c443] | 114 |
|
---|
[1bb2e7a] | 115 | /** @}
|
---|
[b45c443] | 116 | */
|
---|