Index: trunk/hal/generic/hal_gpt.h
===================================================================
--- trunk/hal/generic/hal_gpt.h	(revision 407)
+++ trunk/hal/generic/hal_gpt.h	(revision 408)
@@ -35,5 +35,5 @@
 //   dependent, and is defined as (CONFIG_USER_SPACE_SIZE / CONFIG_PPM_PAGE_SIZE).
 // - Each entry contains a Physical Page Number (ppn_t type), and a set of attributes,
-//   defined as masks on a 32 bits-vector.
+//   defined as a 32 bits-vector.
 //
 // Any arch-specific implementation must implement this API.
@@ -126,5 +126,5 @@
 
 /****************************************************************************************
- * This function maps a page table entry identified by its VPN, from values defined
+ * This function map a local GPT entry identified by its VPN, from values defined
  * by the ppn and attr arguments. It allocates physical memory for the local generic
  * page table itself if required.
@@ -132,12 +132,12 @@
  * @ gpt       : [in] pointer on the page table
  * @ vpn       : [in] virtual page number
+ * @ attr      : [in] generic attributes
  * @ ppn       : [in] physical page number
- * @ attr      : [in] generic attributes
  * @ returns 0 if success / returns ENOMEM if error
  ***************************************************************************************/
 error_t hal_gpt_set_pte( gpt_t    * gpt,
                          vpn_t      vpn,
-                         ppn_t      ppn,
-                         uint32_t   attr );
+                         uint32_t   attr,
+                         ppn_t      ppn );
 
 /****************************************************************************************
@@ -153,8 +153,8 @@
 
 /****************************************************************************************
- * This function returns in the ppn and attr arguments the value of a page table
- * entry identified by its VPN.  It returns attr == 0 if the page is not mapped.
- ****************************************************************************************
- * @ gpt       : [in]  pointer on the page table
+ * This function returns in the <attr> and <ppn> arguments the current values 
+ * stored in a GPT entry, identified by the <gpt> and <vpn> arguments.
+ ****************************************************************************************
+ * @ gpt_xp    : [in]  pointer on the page table
  * @ vpn       : [in]  virtual page number
  * @ attr      : [out] generic attributes
@@ -167,34 +167,85 @@
 
 /****************************************************************************************
- * This function is used to implement the "fork" system call: It copies all valid GPT
- * entries for a given vseg identified by the <vpn_base> and <vpn_size> arguments,
- * from the source <src_gpt> to the <dst_gpt>. 
+ * This function is used to implement the "fork" system call: It copies one GPT entry
+ * identified by the <vpn> argument, from a remote <src_gpt_xp> to a local <dst_gpt>. 
+ * It does nothing if the source PTE is not MAPPED and SMALL.
  * It optionnally activates the "Copy on Write" mechanism: when the <cow> argument is
- * true, the GPT_WRITABLE flag is reset, and the GPT_COW flag is set for each valid 
- * entry in the destination GPT (The data page will be dynamically allocated an copied
- * when a write access is detected).
- ****************************************************************************************
- * @ dst_gpt   : [in]  pointer on the destination GPT.
- * @ src_gpt   : [in]  pointer on the source GPT.
- * @ vpn_base  : [in]  first vpn in vseg.
- * @ vpn_size  : [in]  number of pages in vseg.
- * @ cow       : [in]  activate the COPY-On-Write mechanism if true.
- ***************************************************************************************/
-error_t hal_gpt_copy( gpt_t    * dst_gpt,
-                      gpt_t    * src_gpt,
-                      vpn_t      vpn_base,
-                      vpn_t      vpn_size,
-                      bool_t     cow );
-
-/****************************************************************************************
- * This function returns GPT_COW flag for a PTE defined by <gpt> and <vpn> arguments.
+ * true: the GPT_WRITABLE flag is reset, and the GPT_COW flag is set.
+ * A new second level PT2(s) is allocated for destination GPT if required.
+ * It returns in the <ppn> and <mapped> arguments the PPN value for the copied PTE,
+ * and a boolean indicating if the PTE is mapped and small, and was actually copied.
+ ****************************************************************************************
+ * @ dst_gpt      : [in]  local pointer on the local destination GPT.
+ * @ src_gpt_xp   : [in]  extended pointer on the remote source GPT.
+ * @ vpn_base     : [in]  vpn defining the PTE to be copied.
+ * @ cow          : [in]  activate the COPY-On-Write mechanism if true.
+ * @ ppn          : [out] PPN value (only if mapped is true).
+ * @ mapped       : [out] true if src_gpt[vpn] actually copied to dst_gpt[vpn].
+ * @ return 0 if success / return -1 if no memory for a new PT2.
+ ***************************************************************************************/
+error_t hal_gpt_pte_copy( gpt_t    * dst_gpt,
+                          xptr_t     src_gpt_xp,
+                          vpn_t      vpn,
+                          bool_t     cow,
+                          ppn_t    * ppn,
+                          bool_t   * mapped );
+
+/****************************************************************************************
+ * This function returns true if the MAPPED and SMALL flags are both set
+ * for a PTE defined by <gpt> and <vpn> arguments.
  ****************************************************************************************
  * @ gpt       : [in]  pointer on the page table
  * @ vpn       : [in]  virtual page number
- * @ returns true if GPT_COW is set.
+ * @ returns true if MAPPED is set.
+ ***************************************************************************************/
+bool_t hal_gpt_pte_is_mapped( gpt_t * gpt,
+                              vpn_t   vpn );
+
+/****************************************************************************************
+ * This function returns true if the MAPPED, SMALL, and COW flags are all set
+ * for a PTE defined by <gpt> and <vpn> arguments.
+ ****************************************************************************************
+ * @ gpt       : [in]  pointer on the page table
+ * @ vpn       : [in]  virtual page number
+ * @ returns true if COW is set.
  ***************************************************************************************/
 bool_t hal_gpt_pte_is_cow( gpt_t * gpt,
                            vpn_t   vpn );
 
+/****************************************************************************************
+ * This function atomically flip the COW flag and WRITABLE flag for all PTEs
+ * of a remote GPT identified by the <gpt_xp>, <vpn_base>, and <vpn_size arguments.
+ * - it set COW and reset WRITABLE when <set_cow> argument is true and PTE is WRITABLE.
+ * - it set WRITABLE and reset COW when <set_cow> is false and PTE is COW.
+ * It does nothing if the remote PTE is not MAPPED and SMALL.
+ * It is called when a fork is executed, or when a COW must be resolved.
+ ****************************************************************************************
+ * @ set_cow   : [in]  set COW & reset WRITABLE if true / do the opposite if false.
+ * @ gpt_xp    : [in]  extended pointer on the remote GPT.
+ * @ vpn_base  : [in]  first virtual page.
+ * @ vpn_size  : [in]  number of pages.
+ ***************************************************************************************/
+void hal_gpt_flip_cow( bool_t  set_cow,
+                       xptr_t  gpt_xp,
+                       vpn_t   vpn_base,
+                       vpn_t   vpn_size );
+
+/****************************************************************************************
+ * This function is used to maintain coherence amongst the multiple GPT copies.
+ * It modifies an existing entry identified by the <vpn> argument in a remote GPT
+ * identified by the <gpt_xp> argument, using remote accesses.
+ * It cannot fail, because only MAPPED & SMALL entries are modified.
+ ****************************************************************************************
+ * @ gpt_xp    : [in] extended pointer on the page table
+ * @ vpn       : [in] virtual page number
+ * @ attr      : [in] generic attributes
+ * @ ppn       : [in] physical page number
+ ***************************************************************************************/
+void hal_gpt_update_pte( xptr_t     gpt_xp,
+                         vpn_t      vpn,
+                         uint32_t   attr,
+                         ppn_t      ppn );
+
 
 #endif	/* _GPT_H_ */
+
