| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup kernel_ia32_ddi
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <ddi/ddi.h>
|
|---|
| 36 | #include <arch/ddi/ddi.h>
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <proc/task.h>
|
|---|
| 39 | #include <stddef.h>
|
|---|
| 40 | #include <adt/bitmap.h>
|
|---|
| 41 | #include <mm/slab.h>
|
|---|
| 42 | #include <arch/pm.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <arch/cpu.h>
|
|---|
| 45 | #include <cpu.h>
|
|---|
| 46 | #include <arch.h>
|
|---|
| 47 | #include <align.h>
|
|---|
| 48 |
|
|---|
| 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.
|
|---|
| 55 | *
|
|---|
| 56 | */
|
|---|
| 57 | void io_perm_bitmap_install(void)
|
|---|
| 58 | {
|
|---|
| 59 | /* First, copy the I/O Permission Bitmap. */
|
|---|
| 60 | irq_spinlock_lock(&TASK->lock, false);
|
|---|
| 61 |
|
|---|
| 62 | size_t ver = TASK->arch.iomapver;
|
|---|
| 63 | size_t elements = TASK->arch.iomap.elements;
|
|---|
| 64 |
|
|---|
| 65 | if (elements > 0) {
|
|---|
| 66 | assert(TASK->arch.iomap.bits);
|
|---|
| 67 |
|
|---|
| 68 | bitmap_t iomap;
|
|---|
| 69 | bitmap_initialize(&iomap, TSS_IOMAP_SIZE * 8,
|
|---|
| 70 | CPU->arch.tss->iomap);
|
|---|
| 71 | bitmap_copy(&iomap, &TASK->arch.iomap, elements);
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * Set the trailing bits in the last byte of the map to disable
|
|---|
| 75 | * I/O access.
|
|---|
| 76 | */
|
|---|
| 77 | bitmap_set_range(&iomap, elements,
|
|---|
| 78 | ALIGN_UP(elements, 8) - elements);
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * It is safe to set the trailing eight bits because of the
|
|---|
| 82 | * extra convenience byte in TSS_IOMAP_SIZE.
|
|---|
| 83 | */
|
|---|
| 84 | bitmap_set_range(&iomap, ALIGN_UP(elements, 8), 8);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | irq_spinlock_unlock(&TASK->lock, false);
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | * Second, adjust TSS segment limit.
|
|---|
| 91 | * Take the extra ending byte with all bits set into account.
|
|---|
| 92 | */
|
|---|
| 93 | ptr_16_32_t cpugdtr;
|
|---|
| 94 | gdtr_store(&cpugdtr);
|
|---|
| 95 |
|
|---|
| 96 | descriptor_t *gdt_p = (descriptor_t *) cpugdtr.base;
|
|---|
| 97 | size_t size = bitmap_size(elements);
|
|---|
| 98 | gdt_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE + size);
|
|---|
| 99 | gdtr_load(&cpugdtr);
|
|---|
| 100 |
|
|---|
| 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;
|
|---|
| 106 | tr_load(GDT_SELECTOR(TSS_DES));
|
|---|
| 107 |
|
|---|
| 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 | }
|
|---|
| 114 |
|
|---|
| 115 | /** @}
|
|---|
| 116 | */
|
|---|