Index: trunk/kernel/mm/vmm.h
===================================================================
--- trunk/kernel/mm/vmm.h	(revision 407)
+++ trunk/kernel/mm/vmm.h	(revision 408)
@@ -89,33 +89,39 @@
 /*********************************************************************************************
  * This structure defines the Virtual Memory Manager for a given process in a given cluster.
- * This local VMM provides three main services:
- * 1) It registers all vsegs statically or dynamically defined in the vseg list.
- * 2) It allocates virtual memory space for the STACKS and MMAP vsegs (FILE/ANON/REMOTE).
- * 3) It contains the local copy of the generic page table descriptor.
+ * This local VMM provides four main services:
+ * 1) It registers all vsegs in the local copy of the vseg list (VSL).
+ * 2) It contains the local copy of the generic page table (GPT).
+ * 3) The stack manager dynamically allocates virtual memory space for the STACK vsegs.
+ * 4) The mmap manager dynamically allocates virtual memory for the (FILE/ANON/REMOTE) vsegs.
+ ******************************************************a**************************************
+ * Implementation notes:
+ * 1. The VSL contains only local vsegs, but it is implemented as an xlist, and protected by
+ *    a remote_rwlock, because it can be accessed by a thread running in a remote cluster.
+ *    An exemple is the vmm_fork_copy() function.
+ * 2. In most custers, the VSL and GPT are only partial copies of the reference VSL and GPT
+ *    structures, stored in the reference cluster.
  ********************************************************************************************/
 
 typedef struct vmm_s
 {
-	rwlock_t       vsegs_lock;         /*! lock protecting the vsegs list                   */
-	list_entry_t   vsegs_root;         /*! all vsegs in same process and same cluster       */
-	uint32_t       vsegs_nr;           /*! total number of local vsegs                      */
-
-    gpt_t          gpt;                /*! embedded generic page table descriptor           */
-
-    stack_mgr_t    stack_mgr;          /*! embedded STACK vsegs allocator                   */
-    mmap_mgr_t     mmap_mgr;           /*! embedded MMAP vsegs allocator                    */
-
-	uint32_t       pgfault_nr;         /*! page fault counter (instrumentation)             */
-	uint32_t       u_err_nr;           /*! TODO ??? [AG]                                    */
-	uint32_t       m_err_nr;           /*! TODO ??? [AG]                                    */
-
-    vpn_t          kent_vpn_base;      /*! kentry vseg first page                           */
-    vpn_t          args_vpn_base;      /*! args vseg first page                             */
-    vpn_t          envs_vpn_base;      /*! envs zone first page                             */
-    vpn_t          heap_vpn_base;      /*! envs zone first page                             */
-	vpn_t          code_vpn_base;      /*! code zone first page                             */
-	vpn_t          data_vpn_base;      /*! data zone first page                             */
-
-	intptr_t       entry_point;        /*! main thread entry point                          */
+	remote_rwlock_t  vsegs_lock;         /*! lock protecting the vsegs list                 */
+	xlist_entry_t    vsegs_root;         /*! VSL root (VSL only complete in reference)      */
+	uint32_t         vsegs_nr;           /*! total number of local vsegs                    */
+
+    gpt_t            gpt;                /*! Generic Page Table (complete in reference)     */
+
+    stack_mgr_t      stack_mgr;          /*! embedded STACK vsegs allocator                 */
+    mmap_mgr_t       mmap_mgr;           /*! embedded MMAP vsegs allocator                  */
+
+	uint32_t         pgfault_nr;         /*! page fault counter (instrumentation)           */
+
+    vpn_t            kent_vpn_base;      /*! kentry vseg first page                         */
+    vpn_t            args_vpn_base;      /*! args vseg first page                           */
+    vpn_t            envs_vpn_base;      /*! envs zone first page                           */
+    vpn_t            heap_vpn_base;      /*! envs zone first page                           */
+	vpn_t            code_vpn_base;      /*! code zone first page                           */
+	vpn_t            data_vpn_base;      /*! data zone first page                           */
+
+	intptr_t         entry_point;        /*! main thread entry point                        */
 }
 vmm_t;
@@ -147,21 +153,60 @@
 
 /*********************************************************************************************
- * This function is called by the sys_fork() system call.
- * It copies the content of a parent process descriptor VMM to a child process VMM.
- * - All vsegs registered in the source VSL are copied in the destination VSL. 
- * - All PTEs registered in the source GPT are copied in destination GPT. For all writable
- *   PTEs - but the FILE vsegs - the WRITABLE flag is reset and the COW flag is set in
- *   the destination GPT. 
- *********************************************************************************************
- * @ dst_process   : pointer on destination process descriptor.
- * @ src_process   : pointer on source process descriptor.
+ * This function is called by the process_fork_create() function. It partially copies
+ * the content of a remote parent process VMM to the local child process VMM:
+ * - all DATA, MMAP, REMOTE vsegs registered in the parent VSL are registered in the child
+ *   VSL, and all valid GPT entries in parent GPT are copied to the child GPT. 
+ *   The WRITABLE flag is reset and the COW flag is set in child GPT.
+ * - all CODE vsegs registered in the parent VSL are registered in the child VSL, but the
+ *   GPT entries are not copied in the chilf GPT, that will be dynamically updated from
+ *   the .elf file when a page fault is reported.
+ * - all FILE vsegs registered in the parent VSL are registered in the child VSL, and all
+ *   valid GPT entries in parent GPT are copied to the child GPT. The COW flag is not set.
+ * - no STACK vseg is copied from  parent VMM to child VMM, because the child STACK vseg
+ *   must be copied from the cluster containing the user thread requesting the fork().
+ *********************************************************************************************
+ * @ child_process     : local pointer on local child process descriptor.
+ * @ parent_process_xp : extended pointer on remote parent process descriptor.
  * @ return 0 if success / return ENOMEM if failure.
  ********************************************************************************************/
-error_t vmm_copy( struct process_s * dst_process,
-                  struct process_s * src_process );
-
-/*********************************************************************************************
- * This function removes all vsegs registered in in a virtual memory manager,
- * and releases the memory allocated to the local generic page table.
+error_t vmm_fork_copy( struct process_s * child_process,
+                       xptr_t             parent_process_xp );
+
+/*********************************************************************************************
+ * This function is called by the process_make_fork() function to handle the fork syscall.
+ * It set the COW flag, and reset the WRITABLE flag of all GPT entries of the DATA, MMAP,
+ * and REMOTE vsegs of a process identified by the <process> argument.
+ * It must be called by a thread running in the reference cluster, that contains the complete
+ * list of vsegs. Use the rpc_vmm_set_cow_client() when the calling thread client is remote. 
+ * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
+ * using the list of copies stored in the owner process, and using remote_write accesses to
+ * update the remote GPTs. It cannot fail, as only mapped entries in GPT copies are updated.
+ *********************************************************************************************
+ * @ process   : local pointer on local reference process descriptor.
+ ********************************************************************************************/
+void vmm_set_cow( struct process_s * process );
+
+/*********************************************************************************************
+ * This function is called by the vmm_get_pte() function in case of COW exception.
+ * It modifies both the PPN an the attributes for a GPT entry identified by the <process>
+ * and <vpn> arguments. 
+ * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
+ * using the list of copies stored in the owner process, and using remote_write accesses to
+ * update the remote GPTs. It cannot fail, as only mapped entries in GPT copies are updated.
+ *********************************************************************************************
+ * @ process   : local pointer on local process descriptor.
+ * @ vpn       : PTE index.
+ * @ attr      : PTE / attributes.
+ * @ ppn       : PTE / physical page index.
+ ********************************************************************************************/
+void vmm_update_pte( struct process_s * process,
+                     vpn_t              vpn,
+                     uint32_t           attr,
+                     ppn_t              ppn );
+
+/*********************************************************************************************
+ * This function removes all vsegs registered in in the virtual memory manager of the
+ * process identified by the <process> argument. 
+ * It releases the memory allocated to the local generic page table.
  *********************************************************************************************
  * @ process   : pointer on process descriptor.
@@ -315,6 +360,6 @@
  * @ returns 0 if success / returns ENOMEM if no memory.
  ********************************************************************************************/
-error_t vmm_copy_on_write( struct process_s * process,
-                           vpn_t              vpn );
+error_t vmm_handle_cow( struct process_s * process,
+                        vpn_t              vpn );
 
 /*********************************************************************************************
