syscalls.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. *****************************************************************************
  3. **
  4. ** File : syscalls.c
  5. **
  6. ** Abstract : System Workbench Minimal System calls file
  7. **
  8. ** For more information about which c-functions
  9. ** need which of these lowlevel functions
  10. ** please consult the Newlib libc-manual
  11. **
  12. ** Environment : System Workbench for MCU
  13. **
  14. ** Distribution: The file is distributed ¡°as is,¡± without any warranty
  15. ** of any kind.
  16. **
  17. *****************************************************************************
  18. **
  19. ** <h2><center>&copy; COPYRIGHT(c) 2014 Ac6</center></h2>
  20. **
  21. ** Redistribution and use in source and binary forms, with or without modification,
  22. ** are permitted provided that the following conditions are met:
  23. ** 1. Redistributions of source code must retain the above copyright notice,
  24. ** this list of conditions and the following disclaimer.
  25. ** 2. Redistributions in binary form must reproduce the above copyright notice,
  26. ** this list of conditions and the following disclaimer in the documentation
  27. ** and/or other materials provided with the distribution.
  28. ** 3. Neither the name of Ac6 nor the names of its contributors
  29. ** may be used to endorse or promote products derived from this software
  30. ** without specific prior written permission.
  31. **
  32. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35. ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  36. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  40. ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. **
  43. *****************************************************************************
  44. */
  45. /* Includes */
  46. #include <sys/stat.h>
  47. #include <stdlib.h>
  48. #include <errno.h>
  49. #include <stdio.h>
  50. #include <signal.h>
  51. #include <time.h>
  52. #include <sys/time.h>
  53. #include <sys/times.h>
  54. /* Variables */
  55. //#undef errno
  56. extern int errno;
  57. extern int __io_putchar(int ch) __attribute__((weak));
  58. extern int __io_getchar(void) __attribute__((weak));
  59. register char * stack_ptr asm("sp");
  60. char *__env[1] = { 0 };
  61. char **environ = __env;
  62. /* Functions */
  63. void initialise_monitor_handles()
  64. {
  65. }
  66. int _getpid(void)
  67. {
  68. return 1;
  69. }
  70. int _kill(int pid, int sig)
  71. {
  72. errno = EINVAL;
  73. return -1;
  74. }
  75. void _exit (int status)
  76. {
  77. _kill(status, -1);
  78. while (1) {} /* Make sure we hang here */
  79. }
  80. __attribute__((weak)) int _read(int file, char *ptr, int len)
  81. {
  82. int DataIdx;
  83. for (DataIdx = 0; DataIdx < len; DataIdx++)
  84. {
  85. *ptr++ = __io_getchar();
  86. }
  87. return len;
  88. }
  89. __attribute__((weak)) int _write(int file, char *ptr, int len)
  90. {
  91. int DataIdx;
  92. for (DataIdx = 0; DataIdx < len; DataIdx++)
  93. {
  94. __io_putchar(*ptr++);
  95. }
  96. return len;
  97. }
  98. caddr_t _sbrk(int incr)
  99. {
  100. extern char end asm("end");
  101. static char *heap_end;
  102. char *prev_heap_end;
  103. if (heap_end == 0)
  104. heap_end = &end;
  105. prev_heap_end = heap_end;
  106. if (heap_end + incr > stack_ptr)
  107. {
  108. // write(1, "Heap and stack collision\n", 25);
  109. // abort();
  110. errno = ENOMEM;
  111. return (caddr_t) -1;
  112. }
  113. heap_end += incr;
  114. return (caddr_t) prev_heap_end;
  115. }
  116. int _close(int file)
  117. {
  118. return -1;
  119. }
  120. int _fstat(int file, struct stat *st)
  121. {
  122. st->st_mode = S_IFCHR;
  123. return 0;
  124. }
  125. int _isatty(int file)
  126. {
  127. return 1;
  128. }
  129. int _lseek(int file, int ptr, int dir)
  130. {
  131. return 0;
  132. }
  133. int _open(char *path, int flags, ...)
  134. {
  135. /* Pretend like we always fail */
  136. return -1;
  137. }
  138. int _wait(int *status)
  139. {
  140. errno = ECHILD;
  141. return -1;
  142. }
  143. int _unlink(char *name)
  144. {
  145. errno = ENOENT;
  146. return -1;
  147. }
  148. int _times(struct tms *buf)
  149. {
  150. return -1;
  151. }
  152. int _stat(char *file, struct stat *st)
  153. {
  154. st->st_mode = S_IFCHR;
  155. return 0;
  156. }
  157. int _link(char *old, char *new)
  158. {
  159. errno = EMLINK;
  160. return -1;
  161. }
  162. int _fork(void)
  163. {
  164. errno = EAGAIN;
  165. return -1;
  166. }
  167. int _execve(char *name, char **argv, char **env)
  168. {
  169. errno = ENOMEM;
  170. return -1;
  171. }