| 1 |
/* |
| 2 |
* $Id$ |
| 3 |
* |
| 4 |
* Copyright (C) 2003-2009 JNode.org |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; either version 2.1 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 |
* License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this library; If not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
package org.jnode.vm; |
| 22 |
|
| 23 |
import javax.naming.NamingException; |
| 24 |
|
| 25 |
import org.jnode.naming.InitialNaming; |
| 26 |
import org.jnode.system.IOResource; |
| 27 |
import org.jnode.system.IRQHandler; |
| 28 |
import org.jnode.system.IRQResource; |
| 29 |
import org.jnode.system.MemoryResource; |
| 30 |
import org.jnode.system.MemoryScanner; |
| 31 |
import org.jnode.system.ResourceManager; |
| 32 |
import org.jnode.system.ResourceNotFreeException; |
| 33 |
import org.jnode.system.ResourceOwner; |
| 34 |
import org.jnode.system.ResourcePermission; |
| 35 |
import org.jnode.system.SimpleResourceOwner; |
| 36 |
import org.jnode.annotation.MagicPermission; |
| 37 |
import org.jnode.vm.scheduler.VmProcessor; |
| 38 |
import org.vmmagic.unboxed.Address; |
| 39 |
import org.vmmagic.unboxed.Extent; |
| 40 |
|
| 41 |
/** |
| 42 |
* Default implementation of ResourceManager. |
| 43 |
* |
| 44 |
* @author Ewout Prangsma (epr@users.sourceforge.net) |
| 45 |
*/ |
| 46 |
@MagicPermission |
| 47 |
final class ResourceManagerImpl implements ResourceManager { |
| 48 |
|
| 49 |
private final ResourcePermission IOPORTS_PERM = new ResourcePermission("ioports"); |
| 50 |
private final ResourcePermission MEMSCAN_PERM = new ResourcePermission("memoryScanner"); |
| 51 |
private final MemoryScanner memScan; |
| 52 |
|
| 53 |
/** |
| 54 |
* Hidden constructor. |
| 55 |
*/ |
| 56 |
private ResourceManagerImpl() { |
| 57 |
memScan = new MemoryScannerImpl(); |
| 58 |
} |
| 59 |
|
| 60 |
protected static ResourceManager initialize() { |
| 61 |
try { |
| 62 |
final Address kernelStart = Unsafe.getKernelStart(); |
| 63 |
final Address kernelEnd = Unsafe.getKernelEnd(); |
| 64 |
final Extent kernelSize = kernelEnd.toWord().sub(kernelStart.toWord()).toExtent(); |
| 65 |
MemoryResourceImpl |
| 66 |
.claimMemoryResource(new SimpleResourceOwner("kernel"), kernelStart, kernelSize, MEMMODE_NORMAL); |
| 67 |
|
| 68 |
final Address bootHeapStart = Unsafe.getBootHeapStart(); |
| 69 |
final Address bootHeapEnd = Unsafe.getBootHeapEnd(); |
| 70 |
final Extent bootHeapSize = bootHeapEnd.toWord().sub(bootHeapStart.toWord()).toExtent(); |
| 71 |
MemoryResourceImpl |
| 72 |
.claimMemoryResource(new SimpleResourceOwner("bootheap"), bootHeapStart, bootHeapSize, MEMMODE_NORMAL); |
| 73 |
|
| 74 |
ResourceManager rm = new ResourceManagerImpl(); |
| 75 |
InitialNaming.bind(NAME, rm); |
| 76 |
return rm; |
| 77 |
} catch (NamingException ex) { |
| 78 |
throw new Error("Cannot initialize ResourceManager"); |
| 79 |
} catch (ResourceNotFreeException ex) { |
| 80 |
throw new Error("Claim kernel memory"); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Claim a range of IO ports |
| 86 |
* |
| 87 |
* @param owner |
| 88 |
* @param startPort |
| 89 |
* @param length |
| 90 |
* @return The claimed resource |
| 91 |
* @throws ResourceNotFreeException |
| 92 |
*/ |
| 93 |
public IOResource claimIOResource(ResourceOwner owner, int startPort, int length) |
| 94 |
throws ResourceNotFreeException { |
| 95 |
final SecurityManager sm = System.getSecurityManager(); |
| 96 |
if (sm != null) { |
| 97 |
sm.checkPermission(IOPORTS_PERM); |
| 98 |
} |
| 99 |
return IOResourceImpl.claimIOResource(owner, startPort, length); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Claim a memory region |
| 104 |
* |
| 105 |
* @param owner |
| 106 |
* @param start |
| 107 |
* @param size |
| 108 |
* @param mode |
| 109 |
* @return The claimed resource |
| 110 |
* @throws ResourceNotFreeException |
| 111 |
*/ |
| 112 |
public MemoryResource claimMemoryResource(ResourceOwner owner, Address start, Extent size, int mode) |
| 113 |
throws ResourceNotFreeException { |
| 114 |
return MemoryResourceImpl.claimMemoryResource(owner, start, size, mode); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Claim a memory region |
| 119 |
* |
| 120 |
* @param owner |
| 121 |
* @param start |
| 122 |
* @param size |
| 123 |
* @param mode |
| 124 |
* @return The claimed resource |
| 125 |
* @throws ResourceNotFreeException |
| 126 |
*/ |
| 127 |
public MemoryResource claimMemoryResource(ResourceOwner owner, Address start, int size, int mode) |
| 128 |
throws ResourceNotFreeException { |
| 129 |
return MemoryResourceImpl.claimMemoryResource(owner, start, Extent.fromIntZeroExtend(size), mode); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Register an interrupt handler for a given irq number. |
| 134 |
* |
| 135 |
* @param owner |
| 136 |
* @param irq |
| 137 |
* @param handler |
| 138 |
* @param shared |
| 139 |
* @return True is the handler was set, false if there was already a handler for the given irq |
| 140 |
* number set. |
| 141 |
* @throws ResourceNotFreeException |
| 142 |
*/ |
| 143 |
public IRQResource claimIRQ(ResourceOwner owner, int irq, IRQHandler handler, boolean shared) |
| 144 |
throws ResourceNotFreeException { |
| 145 |
return VmProcessor.current().getIRQManager().claimIRQ(owner, irq, handler, shared); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Create a MemoryResource wrapper around a given byte-array. |
| 150 |
* |
| 151 |
* @param data |
| 152 |
* @return The claimed resource |
| 153 |
*/ |
| 154 |
public MemoryResource asMemoryResource(byte[] data) { |
| 155 |
return new MemoryResourceImpl(data, data.length, 1); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Create a MemoryResource wrapper around a given array. |
| 160 |
* |
| 161 |
* @param data |
| 162 |
* @return The claimed resource |
| 163 |
*/ |
| 164 |
public MemoryResource asMemoryResource(char[] data) { |
| 165 |
return new MemoryResourceImpl(data, data.length, 2); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Create a MemoryResource wrapper around a given array. |
| 170 |
* |
| 171 |
* @param data |
| 172 |
* @return The claimed resource |
| 173 |
*/ |
| 174 |
public MemoryResource asMemoryResource(short[] data) { |
| 175 |
return new MemoryResourceImpl(data, data.length, 2); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Create a MemoryResource wrapper around a given array. |
| 180 |
* |
| 181 |
* @param data |
| 182 |
* @return The claimed resource |
| 183 |
*/ |
| 184 |
public MemoryResource asMemoryResource(int[] data) { |
| 185 |
return new MemoryResourceImpl(data, data.length, 4); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Create a MemoryResource wrapper around a given array. |
| 190 |
* |
| 191 |
* @param data |
| 192 |
* @return The claimed resource |
| 193 |
*/ |
| 194 |
public MemoryResource asMemoryResource(long[] data) { |
| 195 |
return new MemoryResourceImpl(data, data.length, 8); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Create a MemoryResource wrapper around a given array. |
| 200 |
* |
| 201 |
* @param data |
| 202 |
* @return The claimed resource |
| 203 |
*/ |
| 204 |
public MemoryResource asMemoryResource(float[] data) { |
| 205 |
return new MemoryResourceImpl(data, data.length, 4); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Create a MemoryResource wrapper around a given array. |
| 210 |
* |
| 211 |
* @param data |
| 212 |
* @return The claimed resource |
| 213 |
*/ |
| 214 |
public MemoryResource asMemoryResource(double[] data) { |
| 215 |
return new MemoryResourceImpl(data, data.length, 8); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Gets the memory scanner. |
| 220 |
* This method will requires a ResourcePermission("memoryScanner"). |
| 221 |
* |
| 222 |
* @return the memory scanner |
| 223 |
*/ |
| 224 |
public MemoryScanner getMemoryScanner() { |
| 225 |
final SecurityManager sm = System.getSecurityManager(); |
| 226 |
if (sm != null) { |
| 227 |
sm.checkPermission(MEMSCAN_PERM); |
| 228 |
} |
| 229 |
return memScan; |
| 230 |
} |
| 231 |
} |