brk.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2016 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* brk system call for Linux/NDS32.
  6. Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <sys/syscall.h>
  22. /* This must be initialized data because commons can't have aliases. */
  23. void *__curbrk attribute_hidden = 0;
  24. libc_hidden_proto(brk)
  25. int
  26. brk (void *addr)
  27. {
  28. void *newbrk;
  29. #ifdef NDS32_ABI_V0
  30. __asm__ __volatile__ ("move $r0, %1 \n\t" // save the argment in r0
  31. "pushm $r7, $r8 \n\t"
  32. "sethi $r7, hi20(%2)\n\t" // put the syscall number in r7
  33. "ori $r7, $r7, lo12(%2)\n\t"
  34. "syscall 0x7fff \n\t" // do the system call
  35. "popm $r7, $r8 \n\t"
  36. "move %0, $r5 \n\t" // keep the return value
  37. : "=r"(newbrk)
  38. : "r"(addr), "i"(SYS_ify(brk))
  39. : "$r0", "$r5");
  40. #else
  41. __asm__ __volatile__ ("move $r0, %1 \n\t" // save the argment in r0
  42. "syscall %2 \n\t" // do the system call
  43. "move %0, $r0 \n\t" // keep the return value
  44. : "=r"(newbrk)
  45. : "r"(addr), "i"(SYS_ify(brk))
  46. : "$r0");
  47. #endif
  48. __curbrk = newbrk;
  49. if (newbrk < addr)
  50. {
  51. __set_errno (ENOMEM);
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. libc_hidden_def(brk)