ddi.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <ddi/ddi.h>
00036 #include <arch/ddi/ddi.h>
00037 #include <proc/task.h>
00038 #include <arch/types.h>
00039 #include <typedefs.h>
00040 #include <adt/bitmap.h>
00041 #include <mm/slab.h>
00042 #include <arch/pm.h>
00043 #include <errno.h>
00044 #include <arch/cpu.h>
00045 #include <cpu.h>
00046 #include <arch.h>
00047 #include <align.h>
00048 
00059 int ddi_iospace_enable_arch(task_t *task, __address ioaddr, size_t size)
00060 {
00061         count_t bits;
00062 
00063         bits = ioaddr + size;
00064         if (bits > IO_PORTS)
00065                 return ENOENT;
00066 
00067         if (task->arch.iomap.bits < bits) {
00068                 bitmap_t oldiomap;
00069                 __u8 *newmap;
00070         
00071                 /*
00072                  * The I/O permission bitmap is too small and needs to be grown.
00073                  */
00074                 
00075                 newmap = (__u8 *) malloc(BITS2BYTES(bits), FRAME_ATOMIC);
00076                 if (!newmap)
00077                         return ENOMEM;
00078                 
00079                 bitmap_initialize(&oldiomap, task->arch.iomap.map, task->arch.iomap.bits);
00080                 bitmap_initialize(&task->arch.iomap, newmap, bits);
00081 
00082                 /*
00083                  * Mark the new range inaccessible.
00084                  */
00085                 bitmap_set_range(&task->arch.iomap, oldiomap.bits, bits - oldiomap.bits);
00086 
00087                 /*
00088                  * In case there really existed smaller iomap,
00089                  * copy its contents and deallocate it.
00090                  */             
00091                 if (oldiomap.bits) {
00092                         bitmap_copy(&task->arch.iomap, &oldiomap, oldiomap.bits);
00093                         free(oldiomap.map);
00094                 }
00095         }
00096 
00097         /*
00098          * Enable the range and we are done.
00099          */
00100         bitmap_clear_range(&task->arch.iomap, (index_t) ioaddr, (count_t) size);
00101 
00102         /*
00103          * Increment I/O Permission bitmap generation counter.
00104          */
00105         task->arch.iomapver++;
00106 
00107         return 0;
00108 }
00109 
00117 void io_perm_bitmap_install(void)
00118 {
00119         count_t bits;
00120         ptr_16_32_t cpugdtr;
00121         descriptor_t *gdt_p;
00122         count_t ver;
00123 
00124         /* First, copy the I/O Permission Bitmap. */
00125         spinlock_lock(&TASK->lock);
00126         ver = TASK->arch.iomapver;
00127         if ((bits = TASK->arch.iomap.bits)) {
00128                 bitmap_t iomap;
00129         
00130                 ASSERT(TASK->arch.iomap.map);
00131                 bitmap_initialize(&iomap, CPU->arch.tss->iomap, TSS_IOMAP_SIZE * 8);
00132                 bitmap_copy(&iomap, &TASK->arch.iomap, TASK->arch.iomap.bits);
00133                 /*
00134                  * It is safe to set the trailing eight bits because of the extra
00135                  * convenience byte in TSS_IOMAP_SIZE.
00136                  */
00137                 bitmap_set_range(&iomap, ALIGN_UP(TASK->arch.iomap.bits, 8), 8);
00138         }
00139         spinlock_unlock(&TASK->lock);
00140 
00141         /*
00142          * Second, adjust TSS segment limit.
00143          * Take the extra ending byte with all bits set into account.
00144          */
00145         gdtr_store(&cpugdtr);
00146         gdt_p = (descriptor_t *) cpugdtr.base;
00147         gdt_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE + BITS2BYTES(bits));
00148         gdtr_load(&cpugdtr);
00149 
00150         /*
00151          * Before we load new TSS limit, the current TSS descriptor
00152          * type must be changed to describe inactive TSS.
00153          */
00154         gdt_p[TSS_DES].access = AR_PRESENT | AR_TSS | DPL_KERNEL;
00155         tr_load(selector(TSS_DES));
00156         
00157         /*
00158          * Update the generation count so that faults caused by
00159          * early accesses can be serviced.
00160          */
00161         CPU->arch.iomapver_copy = ver;
00162 }
00163 

Generated on Sun Jun 18 16:38:51 2006 for HelenOS Kernel (ia32) by  doxygen 1.4.6