sys_sparc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /* $Id: sys_sparc.c,v 1.57 2002/02/09 19:49:30 davem Exp $
  2. * linux/arch/sparc64/kernel/sys_sparc.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/sparc
  6. * platform.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/fs.h>
  12. #include <linux/file.h>
  13. #include <linux/mm.h>
  14. #include <linux/sem.h>
  15. #include <linux/msg.h>
  16. #include <linux/shm.h>
  17. #include <linux/stat.h>
  18. #include <linux/mman.h>
  19. #include <linux/utsname.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/slab.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/ipc.h>
  25. #include <linux/personality.h>
  26. #include <linux/random.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/ipc.h>
  29. #include <asm/utrap.h>
  30. #include <asm/perfctr.h>
  31. #include <asm/a.out.h>
  32. #include <asm/unistd.h>
  33. /* #define DEBUG_UNIMP_SYSCALL */
  34. asmlinkage unsigned long sys_getpagesize(void)
  35. {
  36. return PAGE_SIZE;
  37. }
  38. #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
  39. #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL))
  40. /* Does addr --> addr+len fall within 4GB of the VA-space hole or
  41. * overflow past the end of the 64-bit address space?
  42. */
  43. static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
  44. {
  45. unsigned long va_exclude_start, va_exclude_end;
  46. va_exclude_start = VA_EXCLUDE_START;
  47. va_exclude_end = VA_EXCLUDE_END;
  48. if (unlikely(len >= va_exclude_start))
  49. return 1;
  50. if (unlikely((addr + len) < addr))
  51. return 1;
  52. if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
  53. ((addr + len) >= va_exclude_start &&
  54. (addr + len) < va_exclude_end)))
  55. return 1;
  56. return 0;
  57. }
  58. /* Does start,end straddle the VA-space hole? */
  59. static inline int straddles_64bit_va_hole(unsigned long start, unsigned long end)
  60. {
  61. unsigned long va_exclude_start, va_exclude_end;
  62. va_exclude_start = VA_EXCLUDE_START;
  63. va_exclude_end = VA_EXCLUDE_END;
  64. if (likely(start < va_exclude_start && end < va_exclude_start))
  65. return 0;
  66. if (likely(start >= va_exclude_end && end >= va_exclude_end))
  67. return 0;
  68. return 1;
  69. }
  70. /* These functions differ from the default implementations in
  71. * mm/mmap.c in two ways:
  72. *
  73. * 1) For file backed MAP_SHARED mmap()'s we D-cache color align,
  74. * for fixed such mappings we just validate what the user gave us.
  75. * 2) For 64-bit tasks we avoid mapping anything within 4GB of
  76. * the spitfire/niagara VA-hole.
  77. */
  78. static inline unsigned long COLOUR_ALIGN(unsigned long addr,
  79. unsigned long pgoff)
  80. {
  81. unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
  82. unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
  83. return base + off;
  84. }
  85. static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
  86. unsigned long pgoff)
  87. {
  88. unsigned long base = addr & ~(SHMLBA-1);
  89. unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
  90. if (base + off <= addr)
  91. return base + off;
  92. return base - off;
  93. }
  94. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  95. {
  96. struct mm_struct *mm = current->mm;
  97. struct vm_area_struct * vma;
  98. unsigned long task_size = TASK_SIZE;
  99. unsigned long start_addr;
  100. int do_color_align;
  101. if (flags & MAP_FIXED) {
  102. /* We do not accept a shared mapping if it would violate
  103. * cache aliasing constraints.
  104. */
  105. if ((flags & MAP_SHARED) &&
  106. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  107. return -EINVAL;
  108. return addr;
  109. }
  110. if (test_thread_flag(TIF_32BIT))
  111. task_size = STACK_TOP32;
  112. if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
  113. return -ENOMEM;
  114. do_color_align = 0;
  115. if (filp || (flags & MAP_SHARED))
  116. do_color_align = 1;
  117. if (addr) {
  118. if (do_color_align)
  119. addr = COLOUR_ALIGN(addr, pgoff);
  120. else
  121. addr = PAGE_ALIGN(addr);
  122. vma = find_vma(mm, addr);
  123. if (task_size - len >= addr &&
  124. (!vma || addr + len <= vma->vm_start))
  125. return addr;
  126. }
  127. if (len > mm->cached_hole_size) {
  128. start_addr = addr = mm->free_area_cache;
  129. } else {
  130. start_addr = addr = TASK_UNMAPPED_BASE;
  131. mm->cached_hole_size = 0;
  132. }
  133. task_size -= len;
  134. full_search:
  135. if (do_color_align)
  136. addr = COLOUR_ALIGN(addr, pgoff);
  137. else
  138. addr = PAGE_ALIGN(addr);
  139. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  140. /* At this point: (!vma || addr < vma->vm_end). */
  141. if (addr < VA_EXCLUDE_START &&
  142. (addr + len) >= VA_EXCLUDE_START) {
  143. addr = VA_EXCLUDE_END;
  144. vma = find_vma(mm, VA_EXCLUDE_END);
  145. }
  146. if (unlikely(task_size < addr)) {
  147. if (start_addr != TASK_UNMAPPED_BASE) {
  148. start_addr = addr = TASK_UNMAPPED_BASE;
  149. mm->cached_hole_size = 0;
  150. goto full_search;
  151. }
  152. return -ENOMEM;
  153. }
  154. if (likely(!vma || addr + len <= vma->vm_start)) {
  155. /*
  156. * Remember the place where we stopped the search:
  157. */
  158. mm->free_area_cache = addr + len;
  159. return addr;
  160. }
  161. if (addr + mm->cached_hole_size < vma->vm_start)
  162. mm->cached_hole_size = vma->vm_start - addr;
  163. addr = vma->vm_end;
  164. if (do_color_align)
  165. addr = COLOUR_ALIGN(addr, pgoff);
  166. }
  167. }
  168. unsigned long
  169. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  170. const unsigned long len, const unsigned long pgoff,
  171. const unsigned long flags)
  172. {
  173. struct vm_area_struct *vma;
  174. struct mm_struct *mm = current->mm;
  175. unsigned long task_size = STACK_TOP32;
  176. unsigned long addr = addr0;
  177. int do_color_align;
  178. /* This should only ever run for 32-bit processes. */
  179. BUG_ON(!test_thread_flag(TIF_32BIT));
  180. if (flags & MAP_FIXED) {
  181. /* We do not accept a shared mapping if it would violate
  182. * cache aliasing constraints.
  183. */
  184. if ((flags & MAP_SHARED) &&
  185. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  186. return -EINVAL;
  187. return addr;
  188. }
  189. if (unlikely(len > task_size))
  190. return -ENOMEM;
  191. do_color_align = 0;
  192. if (filp || (flags & MAP_SHARED))
  193. do_color_align = 1;
  194. /* requesting a specific address */
  195. if (addr) {
  196. if (do_color_align)
  197. addr = COLOUR_ALIGN(addr, pgoff);
  198. else
  199. addr = PAGE_ALIGN(addr);
  200. vma = find_vma(mm, addr);
  201. if (task_size - len >= addr &&
  202. (!vma || addr + len <= vma->vm_start))
  203. return addr;
  204. }
  205. /* check if free_area_cache is useful for us */
  206. if (len <= mm->cached_hole_size) {
  207. mm->cached_hole_size = 0;
  208. mm->free_area_cache = mm->mmap_base;
  209. }
  210. /* either no address requested or can't fit in requested address hole */
  211. addr = mm->free_area_cache;
  212. if (do_color_align) {
  213. unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
  214. addr = base + len;
  215. }
  216. /* make sure it can fit in the remaining address space */
  217. if (likely(addr > len)) {
  218. vma = find_vma(mm, addr-len);
  219. if (!vma || addr <= vma->vm_start) {
  220. /* remember the address as a hint for next time */
  221. return (mm->free_area_cache = addr-len);
  222. }
  223. }
  224. if (unlikely(mm->mmap_base < len))
  225. goto bottomup;
  226. addr = mm->mmap_base-len;
  227. if (do_color_align)
  228. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  229. do {
  230. /*
  231. * Lookup failure means no vma is above this address,
  232. * else if new region fits below vma->vm_start,
  233. * return with success:
  234. */
  235. vma = find_vma(mm, addr);
  236. if (likely(!vma || addr+len <= vma->vm_start)) {
  237. /* remember the address as a hint for next time */
  238. return (mm->free_area_cache = addr);
  239. }
  240. /* remember the largest hole we saw so far */
  241. if (addr + mm->cached_hole_size < vma->vm_start)
  242. mm->cached_hole_size = vma->vm_start - addr;
  243. /* try just below the current vma->vm_start */
  244. addr = vma->vm_start-len;
  245. if (do_color_align)
  246. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  247. } while (likely(len < vma->vm_start));
  248. bottomup:
  249. /*
  250. * A failed mmap() very likely causes application failure,
  251. * so fall back to the bottom-up function here. This scenario
  252. * can happen with large stack limits and large mmap()
  253. * allocations.
  254. */
  255. mm->cached_hole_size = ~0UL;
  256. mm->free_area_cache = TASK_UNMAPPED_BASE;
  257. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  258. /*
  259. * Restore the topdown base:
  260. */
  261. mm->free_area_cache = mm->mmap_base;
  262. mm->cached_hole_size = ~0UL;
  263. return addr;
  264. }
  265. /* Try to align mapping such that we align it as much as possible. */
  266. unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  267. {
  268. unsigned long align_goal, addr = -ENOMEM;
  269. if (flags & MAP_FIXED) {
  270. /* Ok, don't mess with it. */
  271. return get_unmapped_area(NULL, addr, len, pgoff, flags);
  272. }
  273. flags &= ~MAP_SHARED;
  274. align_goal = PAGE_SIZE;
  275. if (len >= (4UL * 1024 * 1024))
  276. align_goal = (4UL * 1024 * 1024);
  277. else if (len >= (512UL * 1024))
  278. align_goal = (512UL * 1024);
  279. else if (len >= (64UL * 1024))
  280. align_goal = (64UL * 1024);
  281. do {
  282. addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
  283. if (!(addr & ~PAGE_MASK)) {
  284. addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
  285. break;
  286. }
  287. if (align_goal == (4UL * 1024 * 1024))
  288. align_goal = (512UL * 1024);
  289. else if (align_goal == (512UL * 1024))
  290. align_goal = (64UL * 1024);
  291. else
  292. align_goal = PAGE_SIZE;
  293. } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
  294. /* Mapping is smaller than 64K or larger areas could not
  295. * be obtained.
  296. */
  297. if (addr & ~PAGE_MASK)
  298. addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
  299. return addr;
  300. }
  301. /* Essentially the same as PowerPC... */
  302. void arch_pick_mmap_layout(struct mm_struct *mm)
  303. {
  304. unsigned long random_factor = 0UL;
  305. if (current->flags & PF_RANDOMIZE) {
  306. random_factor = get_random_int();
  307. if (test_thread_flag(TIF_32BIT))
  308. random_factor &= ((1 * 1024 * 1024) - 1);
  309. else
  310. random_factor = ((random_factor << PAGE_SHIFT) &
  311. 0xffffffffUL);
  312. }
  313. /*
  314. * Fall back to the standard layout if the personality
  315. * bit is set, or if the expected stack growth is unlimited:
  316. */
  317. if (!test_thread_flag(TIF_32BIT) ||
  318. (current->personality & ADDR_COMPAT_LAYOUT) ||
  319. current->signal->rlim[RLIMIT_STACK].rlim_cur == RLIM_INFINITY ||
  320. sysctl_legacy_va_layout) {
  321. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  322. mm->get_unmapped_area = arch_get_unmapped_area;
  323. mm->unmap_area = arch_unmap_area;
  324. } else {
  325. /* We know it's 32-bit */
  326. unsigned long task_size = STACK_TOP32;
  327. unsigned long gap;
  328. gap = current->signal->rlim[RLIMIT_STACK].rlim_cur;
  329. if (gap < 128 * 1024 * 1024)
  330. gap = 128 * 1024 * 1024;
  331. if (gap > (task_size / 6 * 5))
  332. gap = (task_size / 6 * 5);
  333. mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
  334. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  335. mm->unmap_area = arch_unmap_area_topdown;
  336. }
  337. }
  338. asmlinkage unsigned long sparc_brk(unsigned long brk)
  339. {
  340. /* People could try to be nasty and use ta 0x6d in 32bit programs */
  341. if (test_thread_flag(TIF_32BIT) && brk >= STACK_TOP32)
  342. return current->mm->brk;
  343. if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
  344. return current->mm->brk;
  345. return sys_brk(brk);
  346. }
  347. /*
  348. * sys_pipe() is the normal C calling standard for creating
  349. * a pipe. It's not the way unix traditionally does this, though.
  350. */
  351. asmlinkage long sparc_pipe(struct pt_regs *regs)
  352. {
  353. int fd[2];
  354. int error;
  355. error = do_pipe(fd);
  356. if (error)
  357. goto out;
  358. regs->u_regs[UREG_I1] = fd[1];
  359. error = fd[0];
  360. out:
  361. return error;
  362. }
  363. /*
  364. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  365. *
  366. * This is really horribly ugly.
  367. */
  368. asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
  369. unsigned long third, void __user *ptr, long fifth)
  370. {
  371. int err;
  372. /* No need for backward compatibility. We can start fresh... */
  373. if (call <= SEMCTL) {
  374. switch (call) {
  375. case SEMOP:
  376. err = sys_semtimedop(first, ptr,
  377. (unsigned)second, NULL);
  378. goto out;
  379. case SEMTIMEDOP:
  380. err = sys_semtimedop(first, ptr, (unsigned)second,
  381. (const struct timespec __user *) fifth);
  382. goto out;
  383. case SEMGET:
  384. err = sys_semget(first, (int)second, (int)third);
  385. goto out;
  386. case SEMCTL: {
  387. union semun fourth;
  388. err = -EINVAL;
  389. if (!ptr)
  390. goto out;
  391. err = -EFAULT;
  392. if (get_user(fourth.__pad,
  393. (void __user * __user *) ptr))
  394. goto out;
  395. err = sys_semctl(first, (int)second | IPC_64,
  396. (int)third, fourth);
  397. goto out;
  398. }
  399. default:
  400. err = -ENOSYS;
  401. goto out;
  402. };
  403. }
  404. if (call <= MSGCTL) {
  405. switch (call) {
  406. case MSGSND:
  407. err = sys_msgsnd(first, ptr, (size_t)second,
  408. (int)third);
  409. goto out;
  410. case MSGRCV:
  411. err = sys_msgrcv(first, ptr, (size_t)second, fifth,
  412. (int)third);
  413. goto out;
  414. case MSGGET:
  415. err = sys_msgget((key_t)first, (int)second);
  416. goto out;
  417. case MSGCTL:
  418. err = sys_msgctl(first, (int)second | IPC_64, ptr);
  419. goto out;
  420. default:
  421. err = -ENOSYS;
  422. goto out;
  423. };
  424. }
  425. if (call <= SHMCTL) {
  426. switch (call) {
  427. case SHMAT: {
  428. ulong raddr;
  429. err = do_shmat(first, ptr, (int)second, &raddr);
  430. if (!err) {
  431. if (put_user(raddr,
  432. (ulong __user *) third))
  433. err = -EFAULT;
  434. }
  435. goto out;
  436. }
  437. case SHMDT:
  438. err = sys_shmdt(ptr);
  439. goto out;
  440. case SHMGET:
  441. err = sys_shmget(first, (size_t)second, (int)third);
  442. goto out;
  443. case SHMCTL:
  444. err = sys_shmctl(first, (int)second | IPC_64, ptr);
  445. goto out;
  446. default:
  447. err = -ENOSYS;
  448. goto out;
  449. };
  450. } else {
  451. err = -ENOSYS;
  452. }
  453. out:
  454. return err;
  455. }
  456. asmlinkage long sparc64_newuname(struct new_utsname __user *name)
  457. {
  458. int ret = sys_newuname(name);
  459. if (current->personality == PER_LINUX32 && !ret) {
  460. ret = (copy_to_user(name->machine, "sparc\0\0", 8)
  461. ? -EFAULT : 0);
  462. }
  463. return ret;
  464. }
  465. asmlinkage long sparc64_personality(unsigned long personality)
  466. {
  467. int ret;
  468. if (current->personality == PER_LINUX32 &&
  469. personality == PER_LINUX)
  470. personality = PER_LINUX32;
  471. ret = sys_personality(personality);
  472. if (ret == PER_LINUX32)
  473. ret = PER_LINUX;
  474. return ret;
  475. }
  476. int sparc64_mmap_check(unsigned long addr, unsigned long len,
  477. unsigned long flags)
  478. {
  479. if (test_thread_flag(TIF_32BIT)) {
  480. if (len >= STACK_TOP32)
  481. return -EINVAL;
  482. if ((flags & MAP_FIXED) && addr > STACK_TOP32 - len)
  483. return -EINVAL;
  484. } else {
  485. if (len >= VA_EXCLUDE_START)
  486. return -EINVAL;
  487. if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
  488. return -EINVAL;
  489. }
  490. return 0;
  491. }
  492. /* Linux version of mmap */
  493. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  494. unsigned long prot, unsigned long flags, unsigned long fd,
  495. unsigned long off)
  496. {
  497. struct file * file = NULL;
  498. unsigned long retval = -EBADF;
  499. if (!(flags & MAP_ANONYMOUS)) {
  500. file = fget(fd);
  501. if (!file)
  502. goto out;
  503. }
  504. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  505. len = PAGE_ALIGN(len);
  506. down_write(&current->mm->mmap_sem);
  507. retval = do_mmap(file, addr, len, prot, flags, off);
  508. up_write(&current->mm->mmap_sem);
  509. if (file)
  510. fput(file);
  511. out:
  512. return retval;
  513. }
  514. asmlinkage long sys64_munmap(unsigned long addr, size_t len)
  515. {
  516. long ret;
  517. if (invalid_64bit_range(addr, len))
  518. return -EINVAL;
  519. down_write(&current->mm->mmap_sem);
  520. ret = do_munmap(current->mm, addr, len);
  521. up_write(&current->mm->mmap_sem);
  522. return ret;
  523. }
  524. extern unsigned long do_mremap(unsigned long addr,
  525. unsigned long old_len, unsigned long new_len,
  526. unsigned long flags, unsigned long new_addr);
  527. asmlinkage unsigned long sys64_mremap(unsigned long addr,
  528. unsigned long old_len, unsigned long new_len,
  529. unsigned long flags, unsigned long new_addr)
  530. {
  531. struct vm_area_struct *vma;
  532. unsigned long ret = -EINVAL;
  533. if (test_thread_flag(TIF_32BIT))
  534. goto out;
  535. if (unlikely(new_len >= VA_EXCLUDE_START))
  536. goto out;
  537. if (unlikely(invalid_64bit_range(addr, old_len)))
  538. goto out;
  539. down_write(&current->mm->mmap_sem);
  540. if (flags & MREMAP_FIXED) {
  541. if (invalid_64bit_range(new_addr, new_len))
  542. goto out_sem;
  543. } else if (invalid_64bit_range(addr, new_len)) {
  544. unsigned long map_flags = 0;
  545. struct file *file = NULL;
  546. ret = -ENOMEM;
  547. if (!(flags & MREMAP_MAYMOVE))
  548. goto out_sem;
  549. vma = find_vma(current->mm, addr);
  550. if (vma) {
  551. if (vma->vm_flags & VM_SHARED)
  552. map_flags |= MAP_SHARED;
  553. file = vma->vm_file;
  554. }
  555. /* MREMAP_FIXED checked above. */
  556. new_addr = get_unmapped_area(file, addr, new_len,
  557. vma ? vma->vm_pgoff : 0,
  558. map_flags);
  559. ret = new_addr;
  560. if (new_addr & ~PAGE_MASK)
  561. goto out_sem;
  562. flags |= MREMAP_FIXED;
  563. }
  564. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  565. out_sem:
  566. up_write(&current->mm->mmap_sem);
  567. out:
  568. return ret;
  569. }
  570. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  571. asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
  572. {
  573. static int count;
  574. /* Don't make the system unusable, if someone goes stuck */
  575. if (count++ > 5)
  576. return -ENOSYS;
  577. printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
  578. #ifdef DEBUG_UNIMP_SYSCALL
  579. show_regs (regs);
  580. #endif
  581. return -ENOSYS;
  582. }
  583. /* #define DEBUG_SPARC_BREAKPOINT */
  584. asmlinkage void sparc_breakpoint(struct pt_regs *regs)
  585. {
  586. siginfo_t info;
  587. if (test_thread_flag(TIF_32BIT)) {
  588. regs->tpc &= 0xffffffff;
  589. regs->tnpc &= 0xffffffff;
  590. }
  591. #ifdef DEBUG_SPARC_BREAKPOINT
  592. printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
  593. #endif
  594. info.si_signo = SIGTRAP;
  595. info.si_errno = 0;
  596. info.si_code = TRAP_BRKPT;
  597. info.si_addr = (void __user *)regs->tpc;
  598. info.si_trapno = 0;
  599. force_sig_info(SIGTRAP, &info, current);
  600. #ifdef DEBUG_SPARC_BREAKPOINT
  601. printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
  602. #endif
  603. }
  604. extern void check_pending(int signum);
  605. asmlinkage long sys_getdomainname(char __user *name, int len)
  606. {
  607. int nlen, err;
  608. if (len < 0)
  609. return -EINVAL;
  610. down_read(&uts_sem);
  611. nlen = strlen(utsname()->domainname) + 1;
  612. err = -EINVAL;
  613. if (nlen > len)
  614. goto out;
  615. err = -EFAULT;
  616. if (!copy_to_user(name, utsname()->domainname, nlen))
  617. err = 0;
  618. out:
  619. up_read(&uts_sem);
  620. return err;
  621. }
  622. asmlinkage long solaris_syscall(struct pt_regs *regs)
  623. {
  624. static int count;
  625. regs->tpc = regs->tnpc;
  626. regs->tnpc += 4;
  627. if (test_thread_flag(TIF_32BIT)) {
  628. regs->tpc &= 0xffffffff;
  629. regs->tnpc &= 0xffffffff;
  630. }
  631. if (++count <= 5) {
  632. printk ("For Solaris binary emulation you need solaris module loaded\n");
  633. show_regs (regs);
  634. }
  635. send_sig(SIGSEGV, current, 1);
  636. return -ENOSYS;
  637. }
  638. #ifndef CONFIG_SUNOS_EMUL
  639. asmlinkage long sunos_syscall(struct pt_regs *regs)
  640. {
  641. static int count;
  642. regs->tpc = regs->tnpc;
  643. regs->tnpc += 4;
  644. if (test_thread_flag(TIF_32BIT)) {
  645. regs->tpc &= 0xffffffff;
  646. regs->tnpc &= 0xffffffff;
  647. }
  648. if (++count <= 20)
  649. printk ("SunOS binary emulation not compiled in\n");
  650. force_sig(SIGSEGV, current);
  651. return -ENOSYS;
  652. }
  653. #endif
  654. asmlinkage long sys_utrap_install(utrap_entry_t type,
  655. utrap_handler_t new_p,
  656. utrap_handler_t new_d,
  657. utrap_handler_t __user *old_p,
  658. utrap_handler_t __user *old_d)
  659. {
  660. if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
  661. return -EINVAL;
  662. if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
  663. if (old_p) {
  664. if (!current_thread_info()->utraps) {
  665. if (put_user(NULL, old_p))
  666. return -EFAULT;
  667. } else {
  668. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  669. return -EFAULT;
  670. }
  671. }
  672. if (old_d) {
  673. if (put_user(NULL, old_d))
  674. return -EFAULT;
  675. }
  676. return 0;
  677. }
  678. if (!current_thread_info()->utraps) {
  679. current_thread_info()->utraps =
  680. kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
  681. if (!current_thread_info()->utraps)
  682. return -ENOMEM;
  683. current_thread_info()->utraps[0] = 1;
  684. } else {
  685. if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
  686. current_thread_info()->utraps[0] > 1) {
  687. long *p = current_thread_info()->utraps;
  688. current_thread_info()->utraps =
  689. kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
  690. GFP_KERNEL);
  691. if (!current_thread_info()->utraps) {
  692. current_thread_info()->utraps = p;
  693. return -ENOMEM;
  694. }
  695. p[0]--;
  696. current_thread_info()->utraps[0] = 1;
  697. memcpy(current_thread_info()->utraps+1, p+1,
  698. UT_TRAP_INSTRUCTION_31*sizeof(long));
  699. }
  700. }
  701. if (old_p) {
  702. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  703. return -EFAULT;
  704. }
  705. if (old_d) {
  706. if (put_user(NULL, old_d))
  707. return -EFAULT;
  708. }
  709. current_thread_info()->utraps[type] = (long)new_p;
  710. return 0;
  711. }
  712. long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
  713. {
  714. if (model >= 3)
  715. return -EINVAL;
  716. regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
  717. return 0;
  718. }
  719. asmlinkage long sys_rt_sigaction(int sig,
  720. const struct sigaction __user *act,
  721. struct sigaction __user *oact,
  722. void __user *restorer,
  723. size_t sigsetsize)
  724. {
  725. struct k_sigaction new_ka, old_ka;
  726. int ret;
  727. /* XXX: Don't preclude handling different sized sigset_t's. */
  728. if (sigsetsize != sizeof(sigset_t))
  729. return -EINVAL;
  730. if (act) {
  731. new_ka.ka_restorer = restorer;
  732. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  733. return -EFAULT;
  734. }
  735. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  736. if (!ret && oact) {
  737. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  738. return -EFAULT;
  739. }
  740. return ret;
  741. }
  742. /* Invoked by rtrap code to update performance counters in
  743. * user space.
  744. */
  745. asmlinkage void update_perfctrs(void)
  746. {
  747. unsigned long pic, tmp;
  748. read_pic(pic);
  749. tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
  750. __put_user(tmp, current_thread_info()->user_cntd0);
  751. tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
  752. __put_user(tmp, current_thread_info()->user_cntd1);
  753. reset_pic();
  754. }
  755. asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
  756. {
  757. int err = 0;
  758. switch(opcode) {
  759. case PERFCTR_ON:
  760. current_thread_info()->pcr_reg = arg2;
  761. current_thread_info()->user_cntd0 = (u64 __user *) arg0;
  762. current_thread_info()->user_cntd1 = (u64 __user *) arg1;
  763. current_thread_info()->kernel_cntd0 =
  764. current_thread_info()->kernel_cntd1 = 0;
  765. write_pcr(arg2);
  766. reset_pic();
  767. set_thread_flag(TIF_PERFCTR);
  768. break;
  769. case PERFCTR_OFF:
  770. err = -EINVAL;
  771. if (test_thread_flag(TIF_PERFCTR)) {
  772. current_thread_info()->user_cntd0 =
  773. current_thread_info()->user_cntd1 = NULL;
  774. current_thread_info()->pcr_reg = 0;
  775. write_pcr(0);
  776. clear_thread_flag(TIF_PERFCTR);
  777. err = 0;
  778. }
  779. break;
  780. case PERFCTR_READ: {
  781. unsigned long pic, tmp;
  782. if (!test_thread_flag(TIF_PERFCTR)) {
  783. err = -EINVAL;
  784. break;
  785. }
  786. read_pic(pic);
  787. tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
  788. err |= __put_user(tmp, current_thread_info()->user_cntd0);
  789. tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
  790. err |= __put_user(tmp, current_thread_info()->user_cntd1);
  791. reset_pic();
  792. break;
  793. }
  794. case PERFCTR_CLRPIC:
  795. if (!test_thread_flag(TIF_PERFCTR)) {
  796. err = -EINVAL;
  797. break;
  798. }
  799. current_thread_info()->kernel_cntd0 =
  800. current_thread_info()->kernel_cntd1 = 0;
  801. reset_pic();
  802. break;
  803. case PERFCTR_SETPCR: {
  804. u64 __user *user_pcr = (u64 __user *)arg0;
  805. if (!test_thread_flag(TIF_PERFCTR)) {
  806. err = -EINVAL;
  807. break;
  808. }
  809. err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
  810. write_pcr(current_thread_info()->pcr_reg);
  811. current_thread_info()->kernel_cntd0 =
  812. current_thread_info()->kernel_cntd1 = 0;
  813. reset_pic();
  814. break;
  815. }
  816. case PERFCTR_GETPCR: {
  817. u64 __user *user_pcr = (u64 __user *)arg0;
  818. if (!test_thread_flag(TIF_PERFCTR)) {
  819. err = -EINVAL;
  820. break;
  821. }
  822. err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
  823. break;
  824. }
  825. default:
  826. err = -EINVAL;
  827. break;
  828. };
  829. return err;
  830. }
  831. /*
  832. * Do a system call from kernel instead of calling sys_execve so we
  833. * end up with proper pt_regs.
  834. */
  835. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  836. {
  837. long __res;
  838. register long __g1 __asm__ ("g1") = __NR_execve;
  839. register long __o0 __asm__ ("o0") = (long)(filename);
  840. register long __o1 __asm__ ("o1") = (long)(argv);
  841. register long __o2 __asm__ ("o2") = (long)(envp);
  842. asm volatile ("t 0x6d\n\t"
  843. "sub %%g0, %%o0, %0\n\t"
  844. "movcc %%xcc, %%o0, %0\n\t"
  845. : "=r" (__res), "=&r" (__o0)
  846. : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
  847. : "cc");
  848. return __res;
  849. }