home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / mach / 1061 < prev    next >
Encoding:
Text File  |  1992-08-20  |  9.0 KB  |  336 lines

  1. Newsgroups: comp.os.mach
  2. Path: sparky!uunet!gumby!wupost!rice!weimin
  3. From: weimin@rice.edu (Weimin Yu)
  4. Subject: Help wanted on Mach2.5 
  5. Message-ID: <BtAxIq.L6M@rice.edu>
  6. Originator: weimin@tone.cs.rice.edu
  7. Sender: news@rice.edu (News)
  8. Reply-To: weimin@rice.edu (Weimin Yu)
  9. Organization: Rice University
  10. Date: Thu, 20 Aug 1992 21:40:01 GMT
  11. Lines: 323
  12.  
  13.  
  14.  
  15. Hi, I'm a new comer of the Mach circle. We have a Sun3 running Mach2.5
  16. and I'm screwed up by the protection and exception handling mechanism.
  17.  
  18. I wrote a program which is like
  19.  
  20.   error = vm_allocate(task_self(), &addr, NUM_PAGES*vm_page_size, TRUE);
  21.   error = vm_protect(task_self(), bkup, vm_page_size*NUM_PAGES, TRUE,
  22.                VM_PROT_ALL);    
  23.   cthread_fork(foo, 0);
  24.  
  25.   for (;;) {
  26.       i = msg_receive(&in_msg, MSG_OPTION_NONE, 0);
  27.       exc_server(&in_msg, &out_msg);
  28.       i = msg_send(&out_msg, MSG_OPTION_NONE, 0);
  29.     }
  30.  
  31. where foo is 
  32.   for(;;) {
  33.    error = vm_protect(task_self(), addr,
  34.                               NUM_PAGES*vm_page_size, FALSE, VM_PROT_NONE);
  35.     reading;
  36.     writing;
  37. }
  38.  
  39. I have 2 problems. First, after the first loop in foo(), reading will not
  40. generate exception though all the pages have been protected to VM_PROT_NONE.
  41. Second, with a write exception, I change the page's protection to
  42. VM_PROT_ALL and returns with KERN_SUCCESS,  but the following reads still
  43. get the old value, not that I'm trying to written. Are there some bugs in my
  44. program, or the system doesn't behave itself? 
  45.  
  46. If possible, would you please compile and run the following program and
  47. tell me the result?
  48.  
  49. Thank you very much,
  50.  
  51. Weimin
  52. /*----------------------------------------------------------------------------*/
  53. #include  <stdio.h>
  54. #include <mach.h>
  55. #include <mach/message.h>
  56. #include <mach_error.h>
  57. #include <cthreads.h>
  58. #include <servers/netname.h>
  59. #include <mach/mig_errors.h>
  60. #include <sys/types.h>
  61. #include <sys/time.h>
  62. #include <sys/times.h>
  63. #include <sys/timeb.h>
  64.  
  65. #define MAXDATA   20
  66. #define NUM_ACCE  NUM_PAGES/STEP
  67. #define _locked   0
  68. #define _read     1
  69. #define _written  2
  70.  
  71. port_t                 mport, port, eport;
  72. port_name_t            port_set;
  73. vm_address_t           addr, backup, bkup;
  74. memory_object_t        mobj[2], mctrl[2];
  75. int                    dirty_cnt = 0;
  76. int                    flag = 0, init = 0, unlock=0,req=0, finish = 0, ppg, pg;
  77. int                    NUM_PAGES, STEP = 2, _CNT = 3; stop = 0;
  78. int                    exc_cnt = 0;
  79. struct  timeb      tb[200];
  80. struct  tms        tm[200];
  81. struct  page_state {
  82.   int   state, dirty;
  83.   pointer_t    data;
  84. } *pages;
  85.  
  86. extern   boolean_t memory_object_server();
  87.  
  88. struct  simp_msg_struct {
  89.   msg_header_t  h;
  90.   msg_type_t  t;
  91.   int         inline_data[(108-sizeof(msg_header_t)-sizeof(msg_type_t))/4];
  92. } msg_xmt[2], msg_rcv[2];
  93. int      p_cnt = 0;
  94. int      fd;
  95. mutex_t  mtx;
  96.              vm_task_t               target_task;
  97.              vm_address_t            xaddress;               /* in/out */
  98.              vm_size_t               xsize;                  /* out */
  99.              vm_prot_t               xprotection;            /* out */
  100.              vm_prot_t               xmax_protection;        /* out */
  101.              vm_inherit_t            xinheritance;           /* out */
  102.              boolean_t               xshared;                /* out */
  103.              port_t                  xobject_name;           /* out */
  104.              vm_offset_t             xoffset;                /* out */
  105.  
  106. foo()
  107. {
  108.          kern_return_t  error;
  109.      vm_address_t data, ptr1, ptr2, ptr3, ptr4;
  110.      int  cnt,  i, j, k;
  111.  
  112.      for (cnt = 0; cnt < _CNT; cnt++) {
  113.        ftime(&(tb[2*cnt]));
  114.        times(&(tm[2*cnt]));
  115.        error = vm_protect(task_self(), addr,
  116.                   NUM_PAGES*vm_page_size, FALSE, VM_PROT_READ);
  117.        if (error != KERN_SUCCESS) {
  118.          printf("protect errorf\n");
  119.          exit();
  120.        }
  121.        error = vm_copy(task_self(), addr, vm_page_size*NUM_PAGES, bkup);
  122.        if (error != KERN_SUCCESS) {
  123.          printf("vm_copy error %d \n", error);
  124.          exit();
  125.        }
  126.        error = vm_protect(task_self(), addr,
  127.                   NUM_PAGES*vm_page_size, TRUE, VM_PROT_ALL);
  128.        if (error != KERN_SUCCESS) {
  129.          printf("protect error2\n");
  130.          exit();
  131.        }
  132.        error = vm_protect(task_self(), addr,
  133.                               NUM_PAGES*vm_page_size, FALSE, VM_PROT_NONE);
  134.            if (error != KERN_SUCCESS) {
  135.              printf("protect error3\n");
  136.              exit();
  137.            }
  138.        xaddress = addr;
  139.        error = vm_region(task_self(), &xaddress, &xsize, &xprotection,
  140.              &xmax_protection, &xinheritance, &xshared,
  141.              &xobject_name, &xoffset);
  142.          printf("r: %d %d %d  cur_protection%d %d %d\n", error, xaddress, xsize,
  143.             xprotection, xmax_protection, xobject_name);
  144. fflush(stdout);
  145.        for (i = 1; i< NUM_PAGES; i += STEP) {
  146.          int zz;
  147.          data = addr + vm_page_size*i;
  148. printf("Beginning to read at %d:\n", data);
  149. printf("Data read at %d is %d\n", data, *((int*) data));
  150. printf("trying to write %d at %d\n", 2, data);
  151. fflush(stdout);
  152.          (*((int*) data)) = 2;
  153. printf ("read again at %d, value is %d, icreases it\n", data, *((int*) data));
  154. fflush(stdout);
  155.          (*((int*) data))++;
  156. printf ("read again at %d, value is %d\n", data, *((int*) data));
  157. fflush(stdout);
  158.        }
  159.        p_cnt = 0;
  160.          for (i = 0; i< NUM_PAGES; i++)
  161.          if (pages[i].dirty) {
  162.            ptr1 = addr + i*vm_page_size;
  163.            ptr2 = bkup + i*vm_page_size;
  164.            ptr3 = ptr1 + vm_page_size;
  165.            ptr4 = backup + i*vm_page_size;
  166.            /*printf("yy\n");*/
  167.            for (; ptr1 < ptr3; ptr1 += 4) {
  168.          if (*((int*) ptr1) != *((int*) ptr2)) {
  169.            printf("uux %d %d %d %d\n",i, ptr1, *((int*) ptr1), *((int*) ptr2));
  170.          }
  171.          ptr2 += 4;
  172.          ptr4 += 4;
  173.            }
  174.            pages[i].dirty = 0;
  175.            p_cnt++;
  176.            
  177.          }
  178.        ftime(&(tb[2*cnt+1]));
  179.        times(&(tm[2*cnt+1]));
  180.       
  181.        printf("pages modified: %d\n", p_cnt);
  182.      }
  183.      for (cnt = 0; cnt < _CNT; cnt++) {
  184.        i = (tb[2*cnt+1].time - tb[2*cnt+0].time)*1000 
  185.          + tb[2*cnt+1].millitm - tb[2*cnt+0].millitm;
  186.        printf("LOOP %d:\n", cnt+1);
  187.        printf("Elapsed time: %d milliseconds\n", i);
  188.        i = ((float) (tm[2*cnt+1].tms_utime 
  189.              - tm[2*cnt+0].tms_utime)*1000)/60;
  190.        printf("User time: %d milliseconds\n", i);
  191.        i = ((float) (tm[2*cnt+1].tms_stime 
  192.              - tm[2*cnt+0].tms_stime)*1000)/60;
  193.        printf("Sys  time: %d milliseconds\n", i);
  194.      }
  195.      exit();
  196.      
  197.        }
  198.  
  199. main(argc, argv)
  200. int argc;
  201. char **argv;
  202. {
  203.     struct simp_msg_struct    in_msg;
  204.     death_pill_t    out_msg;
  205.     kern_return_t   i, j, error;
  206.  
  207.     NUM_PAGES = 6;
  208.     STEP = 2;
  209.     _CNT = 2;
  210.     if (argc > 1) {
  211.       argc--;
  212.       sscanf(argv[1], "%d", &NUM_PAGES);
  213.     }
  214.     pages = (struct page_state*)malloc(sizeof(struct page_state)*NUM_PAGES);
  215.     if (argc > 1) {
  216.       argc--;
  217.       sscanf(argv[2], "%d", &STEP);
  218.     }
  219.     if (argc > 1) {
  220.       argc--;
  221.       sscanf(argv[3], "%d", &_CNT);
  222.     }
  223.     if (_CNT >100) _CNT = 100;
  224.  
  225.     for (i = 0; i < MAXDATA; i++) {
  226.       pages[i].state = _locked;
  227.       pages[i].dirty = 0;
  228.     }
  229.     error = port_allocate(task_self(), &mport);
  230.     if (error != KERN_SUCCESS) {
  231.       printf("Port allocateed error\n");
  232.       exit();
  233.     }
  234.     error = port_allocate(task_self(), &port);
  235.     if (error != KERN_SUCCESS) {
  236.       printf("Port allocateed error\n");
  237.       exit();
  238.     }
  239.     error = vm_allocate(task_self(),&backup, NUM_PAGES*vm_page_size, TRUE);
  240.     if (error != KERN_SUCCESS) {
  241.       printf("VM_ allocation error\n");
  242.       exit();
  243.     }
  244.     error = vm_allocate(task_self(),&bkup, NUM_PAGES*vm_page_size, TRUE);
  245.     if (error != KERN_SUCCESS) {
  246.       printf("VM_ allocation error\n");
  247.       exit();
  248.     }
  249.     error = vm_allocate(task_self(), &addr, NUM_PAGES*vm_page_size, TRUE);
  250.     if (error != KERN_SUCCESS) {
  251.       printf("VM_ allocation error\n");
  252.       exit();
  253.     }
  254.     error = vm_protect(task_self(), addr, vm_page_size*NUM_PAGES, TRUE,
  255.                   VM_PROT_ALL);
  256.     if (error != KERN_SUCCESS) {
  257.       printf("protect errora\n");
  258.       exit();
  259.     }
  260.     error = vm_protect(task_self(), backup, vm_page_size*NUM_PAGES, TRUE,
  261.                VM_PROT_NONE);
  262.     if (error != KERN_SUCCESS) {
  263.       printf("protect errorb\n");
  264.       exit();
  265.     }
  266.     error = vm_protect(task_self(), bkup, vm_page_size*NUM_PAGES, TRUE,
  267.                VM_PROT_ALL);
  268.     if (error != KERN_SUCCESS) {
  269.       printf("protect errorc\n");
  270.       exit();
  271.     }
  272.     error = task_set_special_port(task_self(), TASK_EXCEPTION_PORT, mport);
  273.     if (error != KERN_SUCCESS) {
  274.       printf("EXC wrong\n");
  275.       exit();
  276.     }
  277.     cthread_fork(foo, 0);
  278.     in_msg.h.msg_size = sizeof(in_msg);
  279.     in_msg.h.msg_local_port = mport;
  280.  
  281.     for (;;) {
  282.       i = msg_receive(&in_msg, MSG_OPTION_NONE, 0);
  283.       exc_server(&in_msg, &out_msg);
  284.       i = msg_send(&out_msg, MSG_OPTION_NONE, 0);
  285.     }
  286. }
  287.  
  288.  
  289. kern_return_t catch_exception_raise(request_port, thread, task,
  290.                     exception, code, subcode)
  291.                  /* Code contains kern_return_t describing error. */
  292.                 /* Subcode contains bad memory address. */
  293. port_t  request_port, thread, task;
  294. int     exception, code, subcode; {
  295.         int   error;
  296.         printf("%d %d %d %d %d %d %d\n", request_port, thread, task,
  297.                     exception, code, subcode, addr);
  298.     if (exception == 1) {
  299.       int pg;
  300.       pg = (subcode - addr) / vm_page_size;
  301.       pages[pg].dirty++;
  302.       error = vm_protect(task_self(), subcode, vm_page_size, FALSE,
  303.                  VM_PROT_ALL);
  304.       if (error != KERN_SUCCESS) {
  305.          printf("protect error in exc handler\n");
  306.          exit();
  307.        }
  308.     }
  309.     return(KERN_SUCCESS);
  310.     
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.