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 <arch.h>
00046 #include <align.h>
00047 
00058 int ddi_iospace_enable_arch(task_t *task, __address ioaddr, size_t size)
00059 {
00060         count_t bits;
00061 
00062         bits = ioaddr + size;
00063         if (bits > IO_PORTS)
00064                 return ENOENT;
00065 
00066         if (task->arch.iomap.bits < bits) {
00067                 bitmap_t oldiomap;
00068                 __u8 *newmap;
00069         
00070                 /*
00071                  * The I/O permission bitmap is too small and needs to be grown.
00072                  */
00073                 
00074                 newmap = (__u8 *) malloc(BITS2BYTES(bits), FRAME_ATOMIC);
00075                 if (!newmap)
00076                         return ENOMEM;
00077                 
00078                 bitmap_initialize(&oldiomap, task->arch.iomap.map, task->arch.iomap.bits);
00079                 bitmap_initialize(&task->arch.iomap, newmap, bits);
00080 
00081                 /*
00082                  * Mark the new range inaccessible.
00083                  */
00084                 bitmap_set_range(&task->arch.iomap, oldiomap.bits, bits - oldiomap.bits);
00085 
00086                 /*
00087                  * In case there really existed smaller iomap,
00088                  * copy its contents and deallocate it.
00089                  */             
00090                 if (oldiomap.bits) {
00091                         bitmap_copy(&task->arch.iomap, &oldiomap, oldiomap.bits);
00092                         free(oldiomap.map);
00093                 }
00094         }
00095 
00096         /*
00097          * Enable the range and we are done.
00098          */
00099         bitmap_clear_range(&task->arch.iomap, (index_t) ioaddr, (count_t) size);
00100 
00101         /*
00102          * Increment I/O Permission bitmap generation counter.
00103          */
00104         task->arch.iomapver++;
00105 
00106         return 0;
00107 }
00108 
00116 void io_perm_bitmap_install(void)
00117 {
00118         count_t bits;
00119         ptr_16_64_t cpugdtr;
00120         descriptor_t *gdt_p;
00121         tss_descriptor_t *tss_desc;
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 will all bits set into account. 
00144          */
00145         gdtr_store(&cpugdtr);
00146         gdt_p = (descriptor_t *) cpugdtr.base;
00147         gdt_tss_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         tss_desc = (tss_descriptor_t *) &gdt_p[TSS_DES];
00155         tss_desc->type = AR_TSS;
00156         tr_load(gdtselector(TSS_DES));
00157         
00158         /*
00159          * Update the generation count so that faults caused by
00160          * early accesses can be serviced.
00161          */
00162         CPU->arch.iomapver_copy = ver;
00163 }
00164 

Generated on Sun Jun 18 16:26:57 2006 for HelenOS Kernel (amd64) by  doxygen 1.4.6