segattach.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. int verbose = 1;
  21. void
  22. main(void)
  23. {
  24. void *seg;
  25. char *p;
  26. seg = segattach(15, "memory", (void*)0x80000000, 1024*1024);
  27. if(seg == (void*)-1){
  28. print("FAIL: segattach: %r\n");
  29. exits("FAIL");
  30. }
  31. if(seg != (void*)0x80000000){
  32. print("FAIL: segattach: wrong segment base for memory class %#p\n", seg);
  33. exits("FAIL");
  34. }
  35. for(p = seg; p < (char*)seg+(1024*1024); p += 512)
  36. *p = 1;
  37. for(p = seg; p < (char*)seg+(1024*1024); p += 512){
  38. if(*p != 1){
  39. print("FAIL: segattach: unable to write bytes in attached segment\n");
  40. exits("FAIL");
  41. }
  42. }
  43. p = seg + 4096*5+10;
  44. if(segfree(p, 4096*10) < 0){
  45. print("FAIL: segfree: %r\n");
  46. exits("FAIL");
  47. }
  48. for(p = seg; p < (char*)seg+(1024*1024); p += 512){
  49. if(*p != 1){
  50. // print("segfree: found clean address at %#p\n", p);
  51. break;
  52. }
  53. }
  54. if(p == (char*)seg+(1024*1024)){
  55. print("FAIL: segfree: no page previously freed had been faulted\n");
  56. exits("FAIL");
  57. }
  58. if(segdetach(seg) < 0){
  59. print("FAIL: segdetach: %r\n");
  60. exits("FAIL");
  61. }
  62. print("PASS\n");
  63. exits("PASS");
  64. }