Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. //*********************************| C_ROUTINE_PF |********************************
  2. //***********************************(sistema.cpp)*********************************
  3. bool c_routine_pf()
  4. {
  5.     vaddr ind_virt = readCR2();
  6.     natl proc = esecuzione->id;
  7.  
  8.     stat();
  9.  
  10.     for (int i = 3; i >= 0; i--) {
  11.         tab_entry d = get_des(proc, i + 1, ind_virt);
  12.         bool bitP = extr_P(d);
  13.         if (!bitP) {
  14.             des_frame *df = swap(proc, i, ind_virt);
  15.             if (!df)
  16.                 return false;
  17.         }
  18.     }
  19.     return true;
  20. }
  21.  
  22. //*************************************| SWAP |************************************
  23. //***********************************(sistema.cpp)*********************************
  24.  
  25. // carica l'entita' del livello specificato, relativa all'indirizzo virtuale
  26. // ind_virt nello spazio di indirizzamento di proc
  27. des_frame* swap(natl proc, int livello, vaddr ind_virt)
  28. {
  29.     tab_entry e = get_des(proc, livello + 1, ind_virt);
  30.     natq m = extr_IND_MASSA(e);
  31.     if (!m) {
  32.         flog(LOG_WARN,
  33.              "indirizzo %p fuori dallo spazio virtuale allocato",
  34.              ind_virt);
  35.         return 0;
  36.     }
  37.     des_frame* df = alloca_frame(proc, livello, ind_virt);
  38.     if (!df) {
  39.         flog(LOG_WARN, "memoria esaurita");
  40.         return 0;
  41.     }
  42.     df->livello = livello;
  43.     df->residente = 0;
  44.     df->processo = proc;
  45.     df->ind_virtuale = ind_virt;
  46.     df->ind_massa = m;
  47.     df->contatore = 0;
  48.     carica(df);
  49.     collega(df);
  50.     return df;
  51. }
  52.  
  53. //*********************************| ALLOCA_FRAME |********************************
  54. //***********************************(sistema.cpp)*********************************
  55.  
  56. // alloca un frame destinato a contenere l'entita' del
  57. // livello specificato, relativa all'indirizzo virtuale ind_virt
  58. // nello spazio di indirizzamento di proc
  59. des_frame* alloca_frame(natl proc, int livello, vaddr ind_virt)
  60. {
  61.     des_frame *df = alloca_frame_libero();
  62.     if (df == 0) {
  63.         df = scegli_vittima(proc, livello, ind_virt);
  64.         if (df == 0)
  65.             return 0;
  66.         bool occorre_salvare = scollega(df);
  67.         if (occorre_salvare)
  68.             scarica(df);
  69.     }
  70.     return df;
  71. }
  72.  
  73. // indice del descrittore del primo frame libero
  74. des_frame* frame_liberi;   
  75. // estrea un frame libero dalla lista, se non vuota
  76. des_frame* alloca_frame_libero()
  77. {
  78.     des_frame* p = frame_liberi;
  79.     if (frame_liberi != 0)
  80.         frame_liberi = frame_liberi->prossimo_libero;
  81.     return p;
  82. }
  83.  
  84. //********************************| SCEGLI_VITTIMA |*******************************
  85. //***********************************(sistema.cpp)*********************************
  86.  
  87. bool vietato(des_frame* df, natl proc, int liv, vaddr ind_virt)
  88. {
  89.     if (df->livello > liv && df->processo == proc &&
  90.         base(df->ind_virtuale, df->livello) == base(ind_virt, df->livello))
  91.         return true;
  92.     return false;
  93. }
  94.  
  95. des_frame* scegli_vittima(natl proc, int liv, vaddr ind_virt)
  96. {
  97.     des_frame *df, *df_vittima;
  98.     df = &vdf[0];
  99.     while ( df < &vdf[N_DF] &&
  100.         (df->residente ||
  101.          vietato(df, proc, liv, ind_virt)))
  102.         df++;
  103.     if (df == &vdf[N_DF]) return 0;
  104.     df_vittima = df;
  105.     for (df++; df < &vdf[N_DF]; df++) {
  106.         if (df->residente ||
  107.             vietato(df, proc, liv, ind_virt))
  108.             continue;
  109.         if (df->contatore < df_vittima->contatore ||
  110.             (df->contatore == df_vittima->contatore &&
  111.              df_vittima->livello > df->livello))
  112.             df_vittima = df;
  113.     }
  114.     return df_vittima;
  115. }
  116.  
  117. //*************************************| STAT |************************************
  118. //***********************************(sistema.cpp)*********************************
  119. void stat()
  120. {
  121.     des_frame *df1, *df2;
  122.     faddr f1, f2;
  123.     bool bitA;
  124.  
  125.     for (natq i = 0; i < N_DF; i++) {
  126.         df1 = &vdf[i];
  127.         if (df1->livello < 1)
  128.             continue;
  129.         f1 = indirizzo_frame(df1);
  130.         for (int j = 0; j < 512; j++) {
  131.             tab_entry& des = get_entry(f1, j);
  132.             if (!extr_P(des))
  133.                 continue;
  134.             bitA = extr_A(des);
  135.             set_A(des, false);
  136.             f2 = extr_IND_FISICO(des);
  137.             df2 = descrittore_frame(f2);
  138.             if (!df2 || df2->residente)
  139.                 continue;
  140.             df2->contatore >>= 1;
  141.             if (bitA)
  142.                 df2->contatore |= 0x80000000;
  143.         }
  144.     }
  145.     invalida_TLB();
  146. }
  147.  
  148. //*********************************| C_ACTIVATE_PE |*******************************
  149. //***********************************(sistema.cpp)*********************************
  150.  
  151. extern "C" void c_activate_pe(void f(int), int a, natl prio, natl liv, natb type)
  152. {
  153.     proc_elem   *p;         // proc_elem per il nuovo processo
  154.     des_proc *self = des_p(esecuzione->id);
  155.  
  156.     if (prio < MIN_PRIORITY) {
  157.         flog(LOG_WARN, "priorita' non valida: %d", prio);
  158.         c_abort_p();
  159.         return;
  160.     }
  161.  
  162.     p = crea_processo(f, a, prio, liv, true);
  163.     if (p == 0)
  164.         goto error1;
  165.  
  166.     if (!aggiungi_pe(p, type) )
  167.         goto error2;
  168.  
  169.     flog(LOG_INFO, "estern=%d entry=%p(%d) prio=%d liv=%d type=%d",
  170.             p->id, f, a, prio, liv, type);
  171.  
  172.     self->contesto[I_RAX] = p->id;
  173.     return;
  174.  
  175. error2: distruggi_processo(p);
  176. error1:
  177.     self->contesto[I_RAX] = 0xFFFFFFFF;
  178.     return;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement