util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <sys/utsname.h>
  12. #include <sys/param.h>
  13. #include <sys/time.h>
  14. #include "asm/types.h"
  15. #include <ctype.h>
  16. #include <signal.h>
  17. #include <wait.h>
  18. #include <errno.h>
  19. #include <stdarg.h>
  20. #include <sched.h>
  21. #include <termios.h>
  22. #include <string.h>
  23. #include "kern_util.h"
  24. #include "user.h"
  25. #include "mem_user.h"
  26. #include "init.h"
  27. #include "ptrace_user.h"
  28. #include "uml-config.h"
  29. #include "os.h"
  30. #include "longjmp.h"
  31. void stack_protections(unsigned long address)
  32. {
  33. int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  34. if(mprotect((void *) address, page_size(), prot) < 0)
  35. panic("protecting stack failed, errno = %d", errno);
  36. }
  37. void task_protections(unsigned long address)
  38. {
  39. unsigned long guard = address + page_size();
  40. unsigned long stack = guard + page_size();
  41. int prot = 0, pages;
  42. #ifdef notdef
  43. if(mprotect((void *) stack, page_size(), prot) < 0)
  44. panic("protecting guard page failed, errno = %d", errno);
  45. #endif
  46. pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
  47. prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  48. if(mprotect((void *) stack, pages * page_size(), prot) < 0)
  49. panic("protecting stack failed, errno = %d", errno);
  50. }
  51. int raw(int fd)
  52. {
  53. struct termios tt;
  54. int err;
  55. CATCH_EINTR(err = tcgetattr(fd, &tt));
  56. if(err < 0)
  57. return -errno;
  58. cfmakeraw(&tt);
  59. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  60. if(err < 0)
  61. return -errno;
  62. /* XXX tcsetattr could have applied only some changes
  63. * (and cfmakeraw() is a set of changes) */
  64. return(0);
  65. }
  66. void setup_machinename(char *machine_out)
  67. {
  68. struct utsname host;
  69. uname(&host);
  70. #ifdef UML_CONFIG_UML_X86
  71. # ifndef UML_CONFIG_64BIT
  72. if (!strcmp(host.machine, "x86_64")) {
  73. strcpy(machine_out, "i686");
  74. return;
  75. }
  76. # else
  77. if (!strcmp(host.machine, "i686")) {
  78. strcpy(machine_out, "x86_64");
  79. return;
  80. }
  81. # endif
  82. #endif
  83. strcpy(machine_out, host.machine);
  84. }
  85. void setup_hostinfo(char *buf, int len)
  86. {
  87. struct utsname host;
  88. uname(&host);
  89. snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
  90. host.release, host.version, host.machine);
  91. }
  92. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  93. {
  94. va_list args;
  95. jmp_buf buf;
  96. int n;
  97. n = UML_SETJMP(&buf);
  98. if(n == 0){
  99. va_start(args, proc);
  100. (*proc)(&buf, &args);
  101. }
  102. va_end(args);
  103. return n;
  104. }