sgx_graphene.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <pal.h>
  14. #include <pal_error.h>
  15. #include <atomic.h>
  16. #include <linux/futex.h>
  17. #include <errno.h>
  18. #include "sgx_internal.h"
  19. #define PRINTBUF_SIZE 256
  20. struct printbuf {
  21. int idx; // current buffer index
  22. int cnt; // total bytes printed so far
  23. char buf[PRINTBUF_SIZE];
  24. };
  25. static int
  26. fputch(void * f, int ch, struct printbuf * b)
  27. {
  28. __UNUSED(f);
  29. b->buf[b->idx++] = ch;
  30. if (b->idx == PRINTBUF_SIZE - 1) {
  31. INLINE_SYSCALL(write, 3, 2, b->buf, b->idx);
  32. b->idx = 0;
  33. }
  34. b->cnt++;
  35. return 0;
  36. }
  37. static int
  38. vprintf(const char * fmt, va_list ap)
  39. {
  40. struct printbuf b;
  41. b.idx = 0;
  42. b.cnt = 0;
  43. vfprintfmt((void *) &fputch, NULL, &b, fmt, ap);
  44. INLINE_SYSCALL(write, 3, 2, b.buf, b.idx);
  45. return b.cnt;
  46. }
  47. int
  48. pal_printf(const char * fmt, ...)
  49. {
  50. va_list ap;
  51. int cnt;
  52. va_start(ap, fmt);
  53. cnt = vprintf(fmt, ap);
  54. va_end(ap);
  55. return cnt;
  56. }