Welcome, Guest. Please login or register.

Author Topic: Fake seglist in C...  (Read 7099 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Fake seglist in C...
« on: August 07, 2007, 04:33:14 PM »
From the CreateProc() autodoc:
...

If you wish to fake a seglist (that will never
    have DOS UnLoadSeg() called on it), use this code:

                DS.L    0   ;Align to longword
                DC.L    16  ;Segment "length" (faked)
                DC.L    0   ;Pointer to next segment
                ...start of code...


Anyone knows how to do this in C without having an array of LONGs with a jmp instruction in start of code (wich would be kind of unecessary and less performant) ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #1 on: August 07, 2007, 05:24:47 PM »
[EDIT]
Wanted to mean "jmp instruction in start of code" not goto int or something... :roll:
You guys are getting tired of this {bleep} arent' you ? 8-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline golem

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 430
    • Show only replies by golem
Re: Fake seglist in C...
« Reply #2 on: August 07, 2007, 05:31:09 PM »
Jose
Why do you want to fake a seglist?
Are you cracking some code or something?
                                                             
A1200 desktop, Blizzard 1260, OS3.9BB2, Indivision Mk II, SCSI Jaz, Ethernet
A1200 desktop, Blizzard 1230, OS3.1, Ethernet
A500, OS1.3
 

Offline Thomas

Re: Fake seglist in C...
« Reply #3 on: August 07, 2007, 06:00:33 PM »
@golem: no cracking, just run a subroutine of your program (instead of an externally loaded program) as a seperate thread.

@Jose: the opcode for JMP is 0x4EF9. Here is an example: http://thomas-rapp.homepage.t-online.de/examples/multi13.c

But please note that this is Kickstart 1.3 and below style of programming. Starting with Kickstart 2.0 you can use CreateNewProc with NP_Entry and don't need to fake anythimg. The same example for Kick 2.0+: http://thomas-rapp.homepage.t-online.de/examples/multi.c

An important note when working with multiple threads is that ANSI runtime routines, especially buffered I/O (printf etc.) are not thread-safe and can give various strange results. You should use the AmigaDOS equivalents (e.g. Printf with a capital "P") instead.

Bye,
Thomas

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #4 on: August 08, 2007, 02:43:27 PM »
@golem

Just wanting to make a piece of code 1.3 compatible:)

@Thomas

That will do :) I know I should use CreateNewProc, it's just that that was the only function call preventing 1.3 compatibility, I'll probably make two versions of the final file...

:pint:
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #5 on: August 08, 2007, 02:49:03 PM »
BTW, doesn't the main process port need to be in the public list for the subtask to be able to find it with FindPort ?
I prefer to send an ini messsage directly to the child's process MsgPort, no need to search for the parents port:)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline mgerics

  • Sr. Member
  • ****
  • Join Date: Jun 2002
  • Posts: 294
    • Show only replies by mgerics
Re: Fake seglist in C...
« Reply #6 on: August 08, 2007, 03:41:23 PM »
Does anyone else here think there isn't anything about the Amiga that Thomas doesn't know ??!??

I would like to personally thank him for his kind sharing of knowledge without any condescending tones. His advice is always spot on.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Fake seglist in C...
« Reply #7 on: August 08, 2007, 03:50:31 PM »
http://www.iki.fi/sintonen/src/ipc/ has some small IPC example. It's rather easy to mess up subprocessing stuff, say for example have race conditions or cases where resources are not released. This IPC example shows one way of handling such things in a reliable way.

Note: This code is OS2++, but it could easily be 1.3 compatible.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Fake seglist in C...
« Reply #8 on: August 08, 2007, 03:53:51 PM »
@Thomas

Your code has a bug, it doesn't clear instruction cache for the jmp instruction. Add
CacheClearE(&segl->jump, 6, CACRF_ClearI); after the jmp has been poked.
[EDIT] Ahh, but this is KS 1.x example... Ok, that won't work. Forget that one.. :-) [/EDIT]

[EDIT2] Here's a KS 1.x compatible cache clear routine:
Code: [Select]

        include "exec/execbase.i"
        include "lvo/exec.i"

MyCacheClearU
        movem.l d0-d1/a0-a1/a6,-(sp)
        pea     (.cleanexit,pc)
        move.l  (4).w,a6
        cmp.w   #37,(LIB_VERSION,a6)
        blo.b   .manualclear
        jmp     (_LVOCacheClearU,a6)
.cleanexit
        movem.l (sp)+,d0-d1/a0-a1/a6
        rts
.manualclear
        move.l  a5,-(sp)
        lea     (.cacheclrsv,pc),a5
        jsr     (_LVOSupervisor,a6)
        move.l  (sp)+,a5
        rts
.cacheclrsv
        btst    #AFB_68020,(AttnFlags+1,a6)
        beq.b   .nocache
        btst    #AFB_68040,(AttnFlags+1,a6)
        beq.b   .is020or030
        ; 040/060
        cpusha  bc
        cinva   bc
        nop
        rte
.is020or030
        movec   cacr,d0
        or.w    #CACRF_ClearI!CACRF_ClearD,d0
        movec   d0,cacr
.nocache
        rte

[/EDIT2]

Also, Wait()ing for signal clears it. Thus it's possible that the subprocess never sees the SIGBREAKF_CTRL_C.

Relying on some Delay() + subprocessing cleaning up in the meanwhile is a bit bad coding style, especially if it ends up doing RemTask() which can potentially lock the system.

Quote
An important note when working with multiple threads is that ANSI runtime routines, especially buffered I/O (printf etc.) are not thread-safe and can give various strange results. You should use the AmigaDOS equivalents (e.g. Printf with a capital "P") instead.

Actually, many AmigaDOS functions aren't thread safe either, especially buffered I/O ones.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Fake seglist in C...
« Reply #9 on: August 08, 2007, 04:02:37 PM »
Quote
BTW, doesn't the main process port need to be in the public list for the subtask to be able to find it with FindPort ?

It does, and it is. amiga.lib CreatePort() routine does it, if you give it the port name.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #10 on: August 09, 2007, 07:53:58 PM »
"It does, and it is. amiga.lib CreatePort() routine does it, if you give it the port name."

Yap, I was just wanted to mean that if one uses the subprocess message port to send the ini message the main process port doesn't need to public.

Anyway, I think I just wanted to know how I could do it, now that I know I don't have the patience to support 1.3:)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #11 on: August 09, 2007, 07:55:26 PM »
@mgeric
In no particular order, Thomas, Piru and Karlos are the biggest contributers to this place. Haven't seen karlos here lately though...
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Fake seglist in C...
« Reply #12 on: August 09, 2007, 08:31:49 PM »
@Piru

Why does it need to clear the cache ? It's not using selfmodified code and the code is already in memory so the cache data should be valid. (?)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Fake seglist in C...
« Reply #13 on: August 09, 2007, 08:37:21 PM »
@Jose
Quote
Why does it need to clear the cache ? It's not using selfmodified code and the code is already in memory so the cache data should be valid. (?)

It's building code to memory. Since it's impossible to know if this particular memory area might already be in instruction cache - containing some other, earlier instructions - it is necessary to clear the cache at least for this area.

From exec.doc:
Quote
Code: [Select]
Some examples of when the cache needs clearing:
   Self modifying code
   Building Jump tables
   Run-time code patches
   Relocating code for use at different addresses.
   Loading code from disk

Note that loading code from disk with LoadSeg() doesn't require separate cache clearing, it handles caches already.
 

Offline nyteschayde

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Re: Fake seglist in C...
« Reply #14 on: March 07, 2013, 06:04:46 AM »
This thread is years old but I have a question or two. So if I understand all of this, there are at least two different ways to run some code in the background. One is using Tasks (ala CreateTask) and another is using processes using CreateProc. Tasks cannot access any file resources and probably has some other limitations as well. Processes can, it seems, but are more difficult to create and use.

I am still trying to understand MsgPort usage well as well. So, looking at includes and autodocs it seems like a Task struct has a tc_UserData value. If you use something like CreateProc or CreateNewProcTags how can you pass data using tc_userData? Possibly the answer is you can't.

Let me retry this question (sorry; I'm trying to understand as I type this), is it possible to pass arguments to your user defined function that gets executed via CreateProc or CreateNewProcTags?

If you can get access to the Task (through FindTask(NULL) or something like it) you may be able to use tc_userData but I don't know how to set this before the user function executes when using Processes.

Otherwise it seems like one might want to use a MsgPort to send the parameters to the user defined function. If so, and I am guessing here, would one's user defined function immediately set up a MsgPort and wait for messages from the creator of the Process bearing the parameters before continuing on to it's real purpose? Is there a better way?
« Last Edit: March 07, 2013, 09:44:34 AM by nyteschayde »
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500