summaryrefslogtreecommitdiff
path: root/nuttx/configs/ea3131/src/up_fillpage.c
blob: 55c378152fd84177b976019015d44a9cbfd59665 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/****************************************************************************
 * configs/ea3131/src/up_fillpage.c
 * arch/arm/src/board/up_fillpage.c
 *
 *   Copyright (C) 2010 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <errno.h>
#include <debug.h>

#include <nuttx/sched.h>
#include <nuttx/page.h>

#ifdef CONFIG_PAGING
#ifdef CONFIG_PAGING_BINPATH
#  include <sys/stat.h>
#  include <sys/types.h>
#  include <stdbool.h>
#  include <unistd.h>
#  include <fcntl.h>
#endif

/****************************************************************************
 * Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

#ifdef CONFIG_PAGING_BINPATH
struct pg_source_s
{
  bool initialized;  /* TRUE: we are initialized */
  int  fd;           /* File descriptor of the nuttx.bin file */
};
#endif

/****************************************************************************
 * Private Data
 ****************************************************************************/

#ifdef CONFIG_PAGING_BINPATH
static struct pg_source_s g_pgsrc;
#endif

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Public Functions
 ****************************************************************************/

 /****************************************************************************
 * Name: up_fillpage()
 *
 * Description:
 *  After a page is allocated and mapped by up_allocpage(), the actual
 *  filling of the page with data from the non-volatile, must be performed
 *  by a separate call to the architecture-specific function, up_fillpage().
 *  This function is non-blocking, it will start an asynchronous page fill.
 *  The common paging logic will provide a callback function, pg_callback,
 *  that will be called when the page fill is finished (or an error occurs).
 *  This callback is assumed to occur from an interrupt level when the
 *  device driver completes the fill operation.
 *
 *  NOTE 1: Allocating and filling a page is a two step process.  up_allocpage()
 *  allocates the page, and up_fillpage() fills it with data from some non-
 *  volatile storage device.  This distinction is made because up_allocpage()
 *  can probably be implemented in board-independent logic whereas up_fillpage()
 *  probably must be implemented as board-specific logic.
 *
 *  NOTE 2: The initial mapping of vpage will be read-able, write-able,
 *  but non-cacheable.  No special actions will be required of
 *  up_fillpage() in order to write into this allocated page.  If the
 *  virtual address maps to a text region, however, this function should
 *  remap the region so that is is read/execute only.  It should be made
 *  cache-able in any case.

 * Input Parameters:
 *   tcb - A reference to the task control block of the task that needs to
 *         have a page fill.  Architecture-specific logic can retrieve page
 *         fault information from the architecture-specific context
 *         information in this TCB to perform the fill.
 *   pg_callbck - The function to be called when the page fill is complete.
 *
 * Returned Value:
 *   This function will return zero (OK) if the page fill was successfully
 *   started (the result of the page fill is passed to the callback function
 *   as the result argument).  A negated errno value may be returned if an
 *   error occurs.  All errors, however, are fatal.
 *
 *   NOTE: -EBUSY has a special meaning. It is used internally to mean that
 *   the callback function has not executed.  Therefore, -EBUSY should
 *   never be provided in the result argument of pg_callback.
 *
 * Assumptions:
 *   - This function is called from the normal tasking context (but
 *     interrupts siabled).  The implementation must take whatever actions
 *     are necessary to assure that the operation is safe within this context.
 *   - Upon return, the caller will sleep waiting for the page fill callback
 *     to occur.  The callback function will perform the wakeup.
 *
 ****************************************************************************/

#ifdef CONFIG_PAGING_BLOCKINGFILL

/* Version 1:  Supports blocking fill operations */

int up_fillpage(FAR _TCB *tcb, FAR void *vpage)
{
#ifdef CONFIG_PAGING_BINPATH
  ssize_t nbytes;
  off_t   offset;
  off_t   pos;
#endif

  pglldbg("TCB: %p vpage: %p far: %08x\n", tcb, vpage, tcb->xcp.far);
  DEBUGASSERT(tcb->xcp.far >= PG_PAGED_VBASE && tcb->xcp.far < PG_PAGED_VEND);

  /* If BINPATH is defined, then it is the full path to a file on a mounted file
   * system.  In this caseinitialization will be deferred until the first
   * time that up_fillpage() is called.  Are we initialized?
   */

#ifdef CONFIG_PAGING_BINPATH

  if (!g_pgsrc.initialized)
    {
      /* Open the selected path for read-only access */

      g_pgsrc.fd = open(CONFIG_PAGING_BINPATH, O_RDONLY);
      DEBUGASSERT(g_pgsrc.fd >= 0);
      g_pgsrc.initialized = true;
    }

  /* Create an offset into the binary image that corresponds to the 
   * virtual address.   File offset 0 corresponds to PG_LOCKED_VBASE.
   */

  offset = (off_t)vpage - PG_LOCKED_VBASE;

  /* Seek to that position */

  pos = lseek(g_pgsrc.fd, offset, SEEK_SET);
  DEBUGASSERT(pos != (off_t)-1);
  
  /* And read the page data from that offset */

  nbytes = read(g_pgsrc.fd, vpage, PAGESIZE);
  DEBUGASSERT(nbytes == PAGESIZE);
  return OK;

#else /* CONFIG_PAGING_BINPATH */

# warning "Not implemented"
  return -ENOSYS;

#endif /* CONFIG_PAGING_BINPATH */
}

#else

/* Version 2:  Supports non-blocking, asynchronous fill operations */

int up_fillpage(FAR _TCB *tcb, FAR void *vpage, up_pgcallback_t pg_callback)
{
  pglldbg("TCB: %p vpage: %d far: %08x\n", tcb, vpage, tcb->xcp.far);
  DEBUGASSERT(tcb->xcp.far >= PG_PAGED_VBASE && tcb->xcp.far < PG_PAGED_VEND);

#ifdef CONFIG_PAGING_BINPATH
#  error "File system-based paging must always be implemented with blocking calls"
#else
#  warning "Not implemented"
#endif

  return -ENOSYS;
}

#endif /* CONFIG_PAGING_BLOCKINGFILL */

/************************************************************************************
 * Name: lpc313x_pginitialize
 *
 * Description:
 *   Set up mass storage device to support on demand paging.
 *
 ************************************************************************************/

void weak_function lpc313x_pginitialize(void)
{
  /* This initialization does nothing in this example setup.  But this function is
   * where you might, for example:
   *
   * - Initialize and configure a mass storage device to support on-demand paging.
   *   This might be, perhaps an SD card or NAND memory.  An SPI FLASH would probably
   *   already have been configured by lpc313x_spiinitialize(void);
   * - Set up resources to support up_fillpage() operation.  For example, perhaps the
   *   the text image is stored in a named binary file.  In this case, the virtual
   *   text addresses might map to offsets into that file.
   * - Do whatever else is necessary to make up_fillpage() ready for the first time
   *   that it is called.
   */

#ifdef CONFIG_PAGING_BINPATH
  /* If BINPATH is defined, then it is the full path to a file on a mounted file
   * system.  However, in this case, initialization will involve some higher level
   * file system operations.  Since this function is called from a low level (before
   * os_start() is even called), it may not be possible to perform file system
   * operations yet.  Therefore, initialization will be deferred until the first
   * time that up_fillpage() is called.
   */
#endif
}

#endif /* CONFIG_PAGING */