001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.runtime;
014    
015    import org.jikesrvm.apt.annotations.GenerateImplementation;
016    import org.jikesrvm.apt.annotations.SysCallTemplate;
017    import org.jikesrvm.scheduler.RVMThread;
018    import org.vmmagic.pragma.Uninterruptible;
019    import org.vmmagic.unboxed.Address;
020    import org.vmmagic.unboxed.Word;
021    import org.vmmagic.unboxed.Extent;
022    import org.vmmagic.unboxed.Offset;
023    
024    /**
025     * Support for lowlevel (ie non-JNI) invocation of C functions with
026     * static addresses.
027     *
028     * All methods of this class have the following signature:
029     * <pre>
030     * public abstract <TYPE> NAME(<args to pass to sysNAME via native calling convention>)
031     * </pre>
032     * which will call the corresponding method in system call trampoline
033     * with the added function address from the boot image.
034     * <p>
035     * NOTE: From the standpoint of the rest of the VM, an invocation
036     * to a method of SysCall is uninterruptible.
037     * <p>
038     * NOTE: There must be a matching field NAMEIP in BootRecord.java
039     *       for each method declared here.
040     */
041    @Uninterruptible
042    @GenerateImplementation("org.jikesrvm.runtime.SysCallImpl")
043    public abstract class SysCall {
044    
045      /**
046       * Actual implementation of the SysCall class
047       */
048      public static final SysCall sysCall;
049    
050      static {
051        try {
052          sysCall = (SysCall)Class.forName("org.jikesrvm.runtime.SysCallImpl").newInstance();
053        } catch (final Exception e) {
054          throw new Error(e);
055        }
056      }
057    
058      // lowlevel write to console
059      @SysCallTemplate
060      public abstract void sysConsoleWriteChar(char v);
061    
062      @SysCallTemplate
063      public abstract void sysConsoleWriteInteger(int value, int hexToo);
064    
065      @SysCallTemplate
066      public abstract void sysConsoleWriteLong(long value, int hexToo);
067    
068      @SysCallTemplate
069      public abstract void sysConsoleWriteDouble(double value, int postDecimalDigits);
070    
071      // startup/shutdown
072      @SysCallTemplate
073      public abstract void sysExit(int value);
074      @SysCallTemplate
075      public abstract int sysArg(int argno, byte[] buf, int buflen);
076    
077      // misc. info on the process -- used in startup/shutdown
078      @SysCallTemplate
079      public abstract int sysGetenv(byte[] varName, byte[] buf, int limit);
080    
081      // memory
082      @SysCallTemplate
083      public abstract void sysCopy(Address dst, Address src, Extent cnt);
084    
085      @SysCallTemplate
086      public abstract Address sysMalloc(int length);
087    
088      @SysCallTemplate
089      public abstract Address sysCalloc(int length);
090    
091      @SysCallTemplate
092      public abstract void sysFree(Address location);
093    
094      @SysCallTemplate
095      public abstract void sysZero(Address dst, Extent cnt);
096    
097      @SysCallTemplate
098      public abstract void sysZeroPages(Address dst, int cnt);
099    
100      @SysCallTemplate
101      public abstract void sysSyncCache(Address address, int size);
102    
103      /*
104       * Interface to performance events
105       */
106      @SysCallTemplate
107      public abstract int sysPerfEventInit(int events);
108      @SysCallTemplate
109      public abstract int sysPerfEventCreate(int id, byte[] name);
110      @SysCallTemplate
111      public abstract void sysPerfEventEnable();
112      @SysCallTemplate
113      public abstract void sysPerfEventDisable();
114      @SysCallTemplate
115      public abstract int sysPerfEventRead(int id, long[] values);
116    
117      // files
118      @SysCallTemplate
119      public abstract int sysStat(byte[] name, int kind);
120    
121      @SysCallTemplate
122      public abstract int sysReadByte(int fd);
123    
124      @SysCallTemplate
125      public abstract int sysWriteByte(int fd, int data);
126    
127      @SysCallTemplate
128      public abstract int sysReadBytes(int fd, Address buf, int cnt);
129    
130      @SysCallTemplate
131      public abstract int sysWriteBytes(int fd, Address buf, int cnt);
132    
133      @SysCallTemplate
134      public abstract int sysBytesAvailable(int fd);
135    
136      @SysCallTemplate
137      public abstract int sysSyncFile(int fd);
138    
139      @SysCallTemplate
140      public abstract int sysSetFdCloseOnExec(int fd);
141    
142      @SysCallTemplate
143      public abstract int sysAccess(byte[] name, int kind);
144    
145      // mmap - memory mapping
146      @SysCallTemplate
147      public abstract Address sysMMap(Address start, Extent length, int protection, int flags, int fd, Offset offset);
148    
149      @SysCallTemplate
150      public abstract Address sysMMapErrno(Address start, Extent length, int protection, int flags, int fd, Offset offset);
151    
152      @SysCallTemplate
153      public abstract int sysMProtect(Address start, Extent length, int prot);
154    
155      @SysCallTemplate
156      public abstract int sysGetPageSize();
157    
158      // threads
159      @SysCallTemplate
160      public abstract int sysNumProcessors();
161    
162      /**
163       * Create a native thread (aka "unix kernel thread", "pthread").
164       * @param tr
165       * @param ip
166       * @param fp
167       * @return native thread's o/s handle
168       */
169      @SysCallTemplate
170      public abstract Word sysThreadCreate(Address tr, Address ip, Address fp);
171    
172      /**
173       * Tells you if the current system supportes sysNativeThreadBind().
174       * @return 1 if it's supported, 0 if it isn't
175       */
176      @SysCallTemplate
177      public abstract int sysThreadBindSupported();
178    
179      @SysCallTemplate
180      public abstract void sysThreadBind(int cpuId);
181    
182      @SysCallTemplate
183      public abstract void sysThreadYield();
184    
185      @SysCallTemplate
186      public abstract Word sysGetThreadId();
187    
188      @SysCallTemplate
189      public abstract void sysSetupHardwareTrapHandler();
190    
191      // This implies that the RVMThread is somehow pinned, or else the
192      // pthread key value gets moved.  (hence RVMThread is @NonMoving)
193      @SysCallTemplate
194      public abstract int sysStashVMThread(RVMThread vmThread);
195      @SysCallTemplate
196      public abstract void sysThreadTerminate();
197      /**
198       * Allocate the space for a pthread_mutex (using malloc) and initialize
199       * it using pthread_mutex_init with the recursive mutex options.  Note:
200       * it is perfectly OK for the C/C++ code that implements this syscall to
201       * use some other locking mechanism (for example, on systems that don't
202       * have recursive mutexes you could imagine the recursive feature to be
203       * emulated).
204       */
205      @SysCallTemplate
206      public abstract Word sysMonitorCreate();
207      /**
208       * Destroy the monitor pointed to by the argument and free its memory
209       * by calling free.
210       */
211      @SysCallTemplate
212      public abstract void sysMonitorDestroy(Word monitor);
213      @SysCallTemplate
214      public abstract void sysMonitorEnter(Word monitor);
215      @SysCallTemplate
216      public abstract void sysMonitorExit(Word monitor);
217      @SysCallTemplate
218      public abstract void sysMonitorTimedWaitAbsolute(Word monitor, long whenWakeupNanos);
219      @SysCallTemplate
220      public abstract void sysMonitorWait(Word monitor);
221      @SysCallTemplate
222      public abstract void sysMonitorBroadcast(Word monitor);
223      // arithmetic
224      @SysCallTemplate
225      public abstract long sysLongDivide(long x, long y);
226    
227      @SysCallTemplate
228      public abstract long sysLongRemainder(long x, long y);
229    
230      @SysCallTemplate
231      public abstract float sysLongToFloat(long x);
232    
233      @SysCallTemplate
234      public abstract double sysLongToDouble(long x);
235    
236      @SysCallTemplate
237      public abstract int sysFloatToInt(float x);
238    
239      @SysCallTemplate
240      public abstract int sysDoubleToInt(double x);
241    
242      @SysCallTemplate
243      public abstract long sysFloatToLong(float x);
244    
245      @SysCallTemplate
246      public abstract long sysDoubleToLong(double x);
247    
248      @SysCallTemplate
249      public abstract double sysDoubleRemainder(double x, double y);
250    
251      /**
252       * Used to parse command line arguments that are
253       * doubles and floats early in booting before it
254       * is safe to call Float.valueOf or Double.valueOf.
255       *
256       * This aborts in case of errors, with an appropriate error message.
257       *
258       * NOTE: this does not support the full Java spec of parsing a string
259       *       into a float.
260       * @param buf a null terminated byte[] that can be parsed
261       *            by strtof()
262       * @return the floating-point value produced by the call to strtof() on buf.
263       */
264      @SysCallTemplate
265      public abstract float sysPrimitiveParseFloat(byte[] buf);
266    
267      /**
268       * Used to parse command line arguments that are
269       * bytes and ints early in booting before it
270       * is safe to call Byte.parseByte or Integer.parseInt.
271       *
272       * This aborts in case of errors, with an appropriate error message.
273       *
274       * @param buf a null terminated byte[] that can be parsed
275       *            by strtol()
276       * @return the int value produced by the call to strtol() on buf.
277       */
278      @SysCallTemplate
279      public abstract int sysPrimitiveParseInt(byte[] buf);
280    
281      /** Parse memory sizes passed as command-line arguments.
282       */
283      @SysCallTemplate
284      public abstract long sysParseMemorySize(byte[] sizeName, byte[] sizeFlag, byte[] defaultFactor, int roundTo,
285                                              byte[] argToken, byte[] subArg);
286    
287      // time
288      @SysCallTemplate
289      public abstract long sysCurrentTimeMillis();
290    
291      @SysCallTemplate
292      public abstract long sysNanoTime();
293    
294      @SysCallTemplate
295      public abstract void sysNanoSleep(long howLongNanos);
296    
297      // shared libraries
298      @SysCallTemplate
299      public abstract Address sysDlopen(byte[] libname);
300    
301      @SysCallTemplate
302      public abstract Address sysDlsym(Address libHandler, byte[] symbolName);
303    
304      // system startup pthread sync. primitives
305      @SysCallTemplate
306      public abstract void sysCreateThreadSpecificDataKeys();
307    
308      // system calls for alignment checking
309      @SysCallTemplate
310      public abstract void sysEnableAlignmentChecking();
311    
312      @SysCallTemplate
313      public abstract void sysDisableAlignmentChecking();
314    
315      @SysCallTemplate
316      public abstract void sysReportAlignmentChecking();
317    
318      @SysCallTemplate
319      public abstract Address gcspyDriverAddStream(Address driver, int id);
320    
321      @SysCallTemplate
322      public abstract void gcspyDriverEndOutput(Address driver);
323    
324      @SysCallTemplate
325      public abstract void gcspyDriverInit(Address driver, int id, Address serverName, Address driverName, Address title,
326                                           Address blockInfo, int tileNum, Address unused, int mainSpace);
327    
328      @SysCallTemplate
329      public abstract void gcspyDriverInitOutput(Address driver);
330    
331      @SysCallTemplate
332      public abstract void gcspyDriverResize(Address driver, int size);
333    
334      @SysCallTemplate
335      public abstract void gcspyDriverSetTileNameRange(Address driver, int i, Address start, Address end);
336    
337      @SysCallTemplate
338      public abstract void gcspyDriverSetTileName(Address driver, int i, Address start, long value);
339    
340      @SysCallTemplate
341      public abstract void gcspyDriverSpaceInfo(Address driver, Address info);
342    
343      @SysCallTemplate
344      public abstract void gcspyDriverStartComm(Address driver);
345    
346      @SysCallTemplate
347      public abstract void gcspyDriverStream(Address driver, int id, int len);
348    
349      @SysCallTemplate
350      public abstract void gcspyDriverStreamByteValue(Address driver, byte value);
351    
352      @SysCallTemplate
353      public abstract void gcspyDriverStreamShortValue(Address driver, short value);
354    
355      @SysCallTemplate
356      public abstract void gcspyDriverStreamIntValue(Address driver, int value);
357    
358      @SysCallTemplate
359      public abstract void gcspyDriverSummary(Address driver, int id, int len);
360    
361      @SysCallTemplate
362      public abstract void gcspyDriverSummaryValue(Address driver, int value);
363    
364      @SysCallTemplate
365      public abstract void gcspyIntWriteControl(Address driver, int id, int tileNum);
366    
367      @SysCallTemplate
368      public abstract Address gcspyMainServerAddDriver(Address addr);
369    
370      @SysCallTemplate
371      public abstract void gcspyMainServerAddEvent(Address server, int event, Address name);
372    
373      @SysCallTemplate
374      public abstract Address gcspyMainServerInit(int port, int len, Address name, int verbose);
375    
376      @SysCallTemplate
377      public abstract int gcspyMainServerIsConnected(Address server, int event);
378    
379      @SysCallTemplate
380      public abstract Address gcspyMainServerOuterLoop();
381    
382      @SysCallTemplate
383      public abstract void gcspyMainServerSafepoint(Address server, int event);
384    
385      @SysCallTemplate
386      public abstract void gcspyMainServerSetGeneralInfo(Address server, Address info);
387    
388      @SysCallTemplate
389      public abstract void gcspyMainServerStartCompensationTimer(Address server);
390    
391      @SysCallTemplate
392      public abstract void gcspyMainServerStopCompensationTimer(Address server);
393    
394      @SysCallTemplate
395      public abstract void gcspyStartserver(Address server, int wait, Address serverOuterLoop);
396    
397      @SysCallTemplate
398      public abstract void gcspyStreamInit(Address stream, int id, int dataType, Address name, int minValue, int maxValue,
399                                           int zeroValue, int defaultValue, Address pre, Address post, int presentation,
400                                           int paintStyle, int maxStreamIndex, int red, int green, int blue);
401    
402      @SysCallTemplate
403      public abstract void gcspyFormatSize(Address buffer, int size);
404    
405      @SysCallTemplate
406      public abstract int gcspySprintf(Address str, Address format, Address value);
407    }
408